2015-11-06 71 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);