2012-07-04 54 views
1

可能重複:
Function Arguments Passing and Return這個Javascript代碼返回 '未定義'

我需要這個

var foo = { 
    bar: function() { 
     return this.baz; 
    }, 
    baz: 1 
}; 
(function() { 
    return typeof arguments[0](); 
})(foo.bar); 

這個片段一點點幫助執行返回 '未定義' 時 有人可以解釋爲什麼這樣嗎?

+1

Hmmmm,我已經看到了這個確切的片段之前,SO ... – leppie

+2

@leppie與谷歌,我發現這個http://stackoverflow.com/q/8587730/995876:P多麼可悲接受的答案有是不正確的:X – Esailija

+0

@Esailija:這甚至不是我記得的那個!這是一週或兩週前。 – leppie

回答

0

您需要爲呼叫使用正確的範圍。顯式設置foo作爲範圍給出了預期的結果。

var foo = { 
    bar: function() { 
     return this.baz; 
    }, 
    baz: 1 
}; 
(function() { 
    return arguments[0].call(foo); 
})(foo.bar);