Is there anything wrong having javascript functions objects-like properties?
function foo(prop) { return foo[prop] }
foo.faa = 'blah';
foo.fee = 'bleh';
In my real case, I'm using as a status message:
(I couldn't paste my function here as S.O. said it was too much code, but it can be found here).
So I can use like this:
if (candidateStatus(candidate) === candidateStatus.ELECTED) {...}
asked Sep 14, 2016 at 17:15
Dan Heller
5941 gold badge5 silver badges18 bronze badges
2 Answers 2
Nope, there is nothing wrong with that. Functions (like almost everything else) in Javascript are objects, and can be treated as such.
answered Sep 14, 2016 at 17:17
Waiski
9,7603 gold badges24 silver badges31 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Nothing wrong this will act like function [object like] properties
function foo(prop) { return foo[prop] }
foo.faa = 'blah';
foo.fee = 'bleh';
// Is same as
function foo(prop) {
this.faa = 'blan';
this.fee = 'bleh';
return foo[prop]; // or we can write foo.prop
}
var newFun = new foo();
console.log(newFun['faa']); // blan
Case this without return statement
function foo() {
this.faa = 'blan';
this.fee = 'bleh';
}
var newFun = new foo();
console.log(newFoo.faa); // blan
answered Sep 14, 2016 at 17:20
KrishCdbry
1,05911 silver badges19 bronze badges
1 Comment
KrishCdbry
Sorry for the typos I am answering from the mobile App. I have updated the answer it will work now
lang-js
$is actually a function. You can call it like$('.selector')but also hosts "static" functions like$.eachand$.ajax.