2009-10-13 119 views
0

如果我有:調用函數範圍之謎

Object._extends = function(Derived, Base) 
{ 
    Derived.prototype = new Base(); 
    Derived.prototype.constructor = Derived; 
    Derived.uber = Derived.prototype; 
} 


var BASE = function() 
{ 
    this._name = "BASE" 
} 

var DERIVED = function() 
{ 
    Object._extends(DERIVED, BASE) 
    this._age = 3; 
} 
// Object._extends(DERIVED, BASE) if I write here all is ok!!! 

alert(new DERIVED()._name) // undefined! 

當我寫Object._extends(DERIVED, BASE)DERIVED功能則_name是不確定的,但如果我寫相同的功能出來那麼它是不是不確定的,但爲什麼呢?

+0

嘗試'Object._extends(this,BASE)' – jantimon 2009-10-13 11:43:38

+0

我不能用'this',因爲我必須設置prototype屬性,它是函數DERIVED的一個屬性 – xdevel2000 2009-10-13 13:01:45

回答

0

當評估「新」時,引擎首先創建一個對象,然後調用它的構造函數,也就是說,在構造函數中調用的「Object._extends」對已經創建的對象沒有影響。