javascript 从函数返回警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11408218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return alert from function
提问by Hello-World
please can you tell me why this wont work.
请你告诉我为什么这行不通。
If I call s.A why wont the alert show.
如果我打电话给 sA 为什么不显示警报。
var s = {
A: function () { alert("test A"); },
B: function () { alert("test B"); }
};
s.A;
thanks
谢谢
回答by tobias86
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:
A
是一个函数。如果你只是说s.A;
你所做的只是发出对什么的引用A
,例如,如果我s.A;
进入 Chrome 的 javaScript 控制台,我会得到以下信息:
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:
现在,如果我说‘sA();’ 我得到了你最初的期望——它触发了这个功能:
回答by oezi
see it working on jsfiddle. you'll have to add braces to s.A
to make it a function-call.
看看它在 jsfiddle 上工作。您必须添加大括号以s.A
使其成为函数调用。
s.A();
回答by jeff
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
:
s.A();