Javascript - 从第三方调用函数

时间:2015-11-06 14:04:49

标签: javascript

为什么这可能是未定义的,我该如何解决?

var a = 1;
var b = 1 + function(){ return 10; }// This could be the wrong way to do it. But it could come from a third party, so it's undeterminded when it's returning. S
console.log(b);

2 个答案:

答案 0 :(得分:0)

如果你想让这个功能起作用,你必须执行它:

var a = 1;
var b = 1 + function(){ return 10; }()// This could be the wrong way to do it. But it could come from a third party, so it's undeterminded when it's returning. S
console.log(b);

注意我在函数后添加了()。

答案 1 :(得分:0)

您必须调用该函数。

var a = 1;
var b = 1 + (function(){ return 10; })()
console.log(b);