2009-10-13 60 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”对已经创建的对象没有影响。