這就是我面臨着JavaScript關閉問題再次發生!
function AAA(){ this.f1 = function(){ /*expecting that f2 will be call as both of then are of same object.*/ console.log(f2(5));// } this.f2 = function(x){ return x; } } x = new AAA(); x.f1(); //ReferenceError: f2 is not defined
它也不工作,
function AAA(){ this.f1 = function(){ /*expecting that f2 will be call as both of then are of same object.*/ console.log(f3(5));// } /*naming the function f3 hope to get function reference, as function on the right side has name now and in the closure of f1, right?*/ this.f2 = function f3(x){ return x; } function f4() {}; } x = new AAA(); x.f1(); //ReferenceError: f3 is not defined
這到底是怎麼回事?除'f4'之外誰在關閉'f1'?我們不能在沒有'this'的情況下調用相同的對象函數嗎?
整整提到的原因qwertymk:分配給`this.f2`不創建一個名爲變量`f2`,但你的代碼試圖訪問名爲`f2`的變量(!)。 – 2011-02-18 10:03:20
@Chistopher當我將函數命名爲f3時,第二個例子會發生什麼? – Zer001 2011-02-20 08:08:47