调用功能范围之谜

时间:2009-10-13 11:40:34

标签: javascript function scope

如果我有:

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未定义,但如果我写出相同的函数,那么它是不是未定义但是为什么?

1 个答案:

答案 0 :(得分:0)

在评估“new”时,引擎首先创建一个对象然后调用其构造函数,即在构造函数中调用的“Object._extends”对已创建的对象没有影响。