-1

Alright, just looking at the question, it seems like it would matter, but lets look at a test I did.

So I created 3 constructor functions. I did the usual Car, then a Mazda, then a MX8 constructor. All of them inherit based on the code in the JSFiddle. Here is a shell of what I did, more detail can be found in the JSFiddle.

By the way I'm a big fan of Object.create which doesn't need any babysitting of anything.

var Car = function () {
    //code here
}

var Mazda = function (type) {
    this.type = type;
    //more code here including a simple method test    
}

var MX8 = function () {
   //more code here
}

Mazda.prototype = new Car();
MX8.prototype = new Mazda("MX8");

var z = new MX8();

//refer to my JSFiddle
z.interior;               // got correct interior
z.type;                   // got correct type ie model
z.warrantyInfo();         // method was correctly inherited
z.speed;                  // got correct speed
z.constructor === MX8;    // false (unless I specify the constructor in the code of which is commented out in my JSFiddle)
dragonore
  • 783
  • 2
  • 11
  • 25
  • 1
    Can you please clarify what your actual question is? – thefourtheye Apr 19 '15 at 09:00
  • Sure, I guess why would it matter, if things are inherited correctly or so it seems they are? – dragonore Apr 19 '15 at 09:01
  • There are plenty of blog posts and websites and Youtube videos that explain Javascript basics. Not to mention that I agree with @thefourtheye - it is unclear what you are asking. The headline already makes no sense, objects don't return anything! They are objects, passive storage spaces. – Mörre Apr 19 '15 at 09:05
  • 1
    `constructor` is of no significance to JS itself if that's what you mean. Related: http://stackoverflow.com/q/20830449/218196 – Felix Kling Apr 19 '15 at 09:09
  • 1
    http://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor -----http://stackoverflow.com/questions/8285575/javascript-why-this-loop-instance-prototype-property-constructor-property/19616652#19616652 and many more. – Mörre Apr 19 '15 at 09:23
  • I will bookmark that Morre and yours as well Felix. – dragonore Apr 19 '15 at 09:27

1 Answers1

1

short answer:
You need to explicitly set the constructor.

function Base() {}
function Derived() {}

// old prototype object is gone, including constructor property
// it will get however all the properties attached by Base constructor
Derived.prototype = new Base(); 

Derived.prototype.constructor = Derived;

Does it matter if my Object returns the wrong constructor?

Well, it depends on whether you're using that property or not. I'd say it's a good practice to keep the correct constructor.

Possible use case:

function getType (target) {
  // name is empty unless you use the function declaration syntax 
  // (read-only property) 
  return target.constructor.name; 
}

getType(new Derived()) // "Derived"
getType(new Base()) // "Base"

side note:
There are better ways of implementing inheritance in JS. My favorite pattern is the following:

function Base (x) { this.x = x; }

function Derived (x, y) { Base.call(this, x); this.y = y; }

// creates an empty object whose internal "[[Prototype]]" property 
// points to Base.prototype
Derived.prototype = Object.create(Base.prototype);
Derived.prototype.constructor = Derived;

The ideea behind Object.create is based on:

function create (proto) {
  function f () {}
  f.prototype = proto;
  return new f();
}

The actual function Object.create is better because you can pass null as prototype, which doesn't work using the above code. Anyway, you should watch this excellent playlist: Crockford on JavaScript.

  • Oh I agree, I would maintain the constructor myself, just because its as you say "good practice". I just kept seeing videos and blogs talking about how the objects are not inheriting correctly with wrong constructor when things seem to inherit fine for me. That is why I didn't see this as a bad question, but maybe it is. – dragonore Apr 19 '15 at 09:20
  • Thanks for the resource. Yeah, like others before me, I am weaning off of the word "new" and classical inheritence way of things. However as you know, some of this stuff is out in the wild, so its still relevant in my oppinion to know such things. – dragonore Apr 19 '15 at 09:54