最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript inheritance extend function - Stack Overflow

programmeradmin7浏览0评论

I'm having some trouble understanding the IF clause at the end of this function from Pro JavaScript Design Patterns:

function extend(subClass, superClass) {
    var F = function() {};
    F.prototype = superClass.prototype;
    subClass.prototype = new F();
    subClass.prototype.constructor = subClass;

    subClass.superclass = superClass.prototype;
    if(superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
    }
}

The book explains that these lines ensure that the superclass's constructor attribute is correctly set, even if the superclass is the Object class itself. Yet, if I omit those three lines and do the following:

function SubClass() {};
extend(SubClass, Object);

alert(Object.prototype.constructor == Object);

The alert says 'true', which means the superclass's constructor is set correctly even without those last three lines. Under what conditions, then, does this IF statement do something useful?

Thanks.

I'm having some trouble understanding the IF clause at the end of this function from Pro JavaScript Design Patterns:

function extend(subClass, superClass) {
    var F = function() {};
    F.prototype = superClass.prototype;
    subClass.prototype = new F();
    subClass.prototype.constructor = subClass;

    subClass.superclass = superClass.prototype;
    if(superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
    }
}

The book explains that these lines ensure that the superclass's constructor attribute is correctly set, even if the superclass is the Object class itself. Yet, if I omit those three lines and do the following:

function SubClass() {};
extend(SubClass, Object);

alert(Object.prototype.constructor == Object);

The alert says 'true', which means the superclass's constructor is set correctly even without those last three lines. Under what conditions, then, does this IF statement do something useful?

Thanks.

Share Improve this question edited Apr 21, 2010 at 20:20 Leo 38.2k8 gold badges78 silver badges101 bronze badges asked Apr 21, 2010 at 20:19 ZachZach 4232 gold badges4 silver badges10 bronze badges 1
  • I have a question: why the intermediary function F, instead of just subClass.prototype = new superClass();? – 755 Commented Feb 19, 2013 at 10:00
Add a ment  | 

1 Answer 1

Reset to default 15

The problem that those two lines try to avoid, is generally produced when you replace the prototype property of a Constructor Function, for example:

function Foo () {};
Foo.prototype = {
  bar: 'baz'
};

var foo = new Foo();
foo.constructor === Object; // true, but `constructor` should refer to Foo

When functions objects are created, the prototype property is initialized with a new object, which contains a constructor property that refers to the function itself, e.g.:

function Bar () {};
var bar = new Bar();
bar.constructor === Bar; // true

When you replace the prototype property with another object, this object has it's own constructor property, generally inherited from other constructor, or from Object.prototype.

var newObj = {};
newObj.constructor === Object;

Remended articles:

  • Constructors considered mildly confusing
  • JavaScript Prototypal Inheritance
发布评论

评论列表(0)

  1. 暂无评论