5

In JavaScript, every function's prototype object has a non-enumerable property constructor which points to the function (EcmaScript §13.2). It is not used in any native functionality (e.g. instanceof checks only the prototype chain), however we are encouraged to adjust it when overwriting the prototype property of a function for inheritance:

SubClass.prototype = Object.create(SuperClass.prototype, {
    constructor: {value:SubClass, writable:true, configurable:true}
});

But, do we (including me) do that only for clarity and neatness? Are there any real-world use cases that rely on the constructor property?

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Whats my understanding constructor property is used to see whether a particular object is created or constructed by which functional constructor. – Apurv Sep 27 '12 at 13:10
  • related: [Is there a good use case for the constructor property in Javascript?](http://stackoverflow.com/questions/8073867/is-there-a-good-use-case-for-the-constructor-property-in-javascript), although the answers are not helpful – Bergi Apr 03 '13 at 10:19
  • see also: [Why is it necessary to set the prototype constructor?](http://stackoverflow.com/q/8453887/1048572) – Bergi Jul 21 '15 at 21:23

2 Answers2

2

I can't really see why the constructor property is what it is in JS. I occasionally find myself using it to get to the prototypes of objects (like the Event object) in IE < 9. However I do think it's there to allow some ppl to mimic classical OO programming constructs:

function Foo()
{
    this.name = 'Foo';
}
function Bar()
{
    this.name = 'Bar';
}
function Foobar(){};
Foo.prototype = new Foobar;
Foo.prototype.constructor = Foo;
Bar.prototype = new Foobar;
Bar.prototype.constructor = Bar;
var foo = new Foo();
var bar = new Bar();
//so far the set-up
function pseudoOverload(obj)
{
    if (!(obj instanceof Foobar))
    {
        throw new Error 'I only take subclasses of Foobar';
    }
    if (obj.constructor.name === 'Foo')
    {
        return new obj.constructor;//reference to constructor is quite handy
    }
    //do stuff with Bar instance
}

So AFAIK, the "advantages" of the constructor property are:

  • instantiating new objects from instance easily
  • being able to group your objects as being subclasses of a certain class, but still being able to check what particular type of subclass you're dealing with.
  • As you say: being tidy.
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Whats my understanding constructor property is used to see whether a particular object is created or constructed by which functional constructor.

This is a great example for the same: http://www.klauskomenda.com/code/javascript-inheritance-by-example/

Apurv
  • 309
  • 2
  • 10
  • That article does not use the `constructor` property at all. You might want to read http://joost.zeekat.nl/constructors-considered-mildly-confusing.html to understand what I'm asking for :-) – Bergi Sep 27 '12 at 13:17
  • Sorry i mean prototype..my bad :) – Apurv Sep 27 '12 at 14:01