Hello-World
Hello-World

Reputation: 9555

return alert from function

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

Answers (3)

tobias86
tobias86

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:Chrome Screenshot

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:

enter image description here

Upvotes: 8

jeff
jeff

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

oezi
oezi

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

Related Questions