Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Quick background: In JavaScript, the constructor function for each object type has a prototype property. The prototype refers to an object that each constructed object uses as the next step up in its prototype chain. When you want one type to inherent from another type, you can set the prototype of the child type to a new instance of the parent type.

For example:

var Parent = function() { /* constructor business */ }
Parent.prototype.parentProp = "some parent property";

var Child = function() { /* constructor business */ }
Child.prototype = /*** !! Some prototype object goes here !! ***/

My question asks about what code should go in the "Some prototype object goes here" spot in the above code. My first instinct is to construct an instance of the parent (i.e., new Parent()), but in a comment to an answer on Is this a safe way of copying one objects prototype to another?, one user writes:

No, do not use new bar() for the prototype object!

(...which is an opinion I've seen in many SO answers and comments, but this is the only example I have on hand at the moment.)

The other option is to use Object.create(Parent.prototype) as Child.prototype. As far as I know, this also creates a new Parent instance, but it does not run the Parent constructor.

Can someone explain why running the constructor function should be avoided when generating a prototype object from a parent type? Is there some significant technical problem that arises (perhaps with multiple levels of inheritance)? Or is such a pattern a misuse of constructors that clashes with some prototypical best practice (e.g., running the constructor when creating a prototype violates some separation of concerns)?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Because it's a function that doesn't need to be called. new is not that different from a regular function call in Javascript.

A constructor can do more than simply set fields. For instance, if it validates incoming data, then you will cause a validation error when you were simply trying to set inheritance chain.

And you don't need Object.create, this is sufficient:

function objectCreate( proto ) {
    function T(){}
    T.prototype = proto;
    return new T();
}
share|improve this answer
    
But that's exactly how Object.create is implemented. –  Casey Chu May 15 '13 at 20:52
    
@CaseyChu not at all. And I mentioned it because in the linked post someone said "Object.create doesn't work in IE8" - which is just useless comment when you can implement it for this use case in 2 seconds on any browser. –  Esailija May 15 '13 at 21:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.