views:

47

answers:

1

If I have:

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!

when I write Object._extends(DERIVED, BASE) into DERIVED function then _name is undefined but if I write the same function out then it is not undefined but why?

A: 

When evaluating "new", engine first creates an object and then calls its constructor function, that is, "Object._extends" called in constructor has no effect on already created object.

stereofrog