Reputation: 9555
please can you tell me why this wont work.
If I call s.A why wont the alert show.
var s = {
A: function () { alert("test A"); },
B: function () { alert("test B"); }
};
s.A;
thanks
Upvotes: 1
Views: 18562
Reputation: 5029
Try
s.A();
A
is a function. If you just say s.A;
all you're doing is emitting the reference to what A
is, e.g. if I whack s.A;
into Chrome's javaScript console I get the following:
Notice how all it did was output the function definition?
Now, if I say `s.A();' I get what you originally expected - it fires the function:
Upvotes: 8
Reputation: 8348
You're returning a reference to the function, but it's not being called. To do so, add the braces after s.A
:
s.A();
Upvotes: 1
Reputation: 51817
see it working on jsfiddle. you'll have to add braces to s.A
to make it a function-call.
s.A();
Upvotes: 3