2015-11-06 44 views
-1

为什么这可能是未定义的,我将如何修复它?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); 
+0

这不是完全清楚你问 - 你可以提供一个更好的例子吗?在你给的那个函数中,它会简单地记录''1function(){return 10;}“' –

+0

你正在接受一个函数(不是返回值)并且给它加1。 '1+函数'是无效的,并给你未定义。你可以将你的函数封装在一个立即执行的函数中:'1 +(function(){return 10;})();' – h2ooooooo

+2

这与第三方函数有什么关系?你在哪里弄不明白?我觉得很多东西在这里不见了。 –

回答

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); 

通知。

0

您将不得不调用该函数。

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