3

Here's the code for doing this

function Person() {
    this.klass = 'human';
}

Person.prototype.toString = function () {
    return this.klass;
};

Person.prototype.greeting = function () {
    return 'hello everyone Im ' + this.name + ', my job is ' + this.klass + '。';
};

function Programmer(name) {
    this.name = name;
    this.klass = 'coder';
}

Programmer.prototype = new Person();
Programmer.prototype.constructor = Programmer;


var someone = new Programmer('sam');

someone.name;          // sam
someone.toString();    // coder
someone.greeting();    // hello everyone Im sam my job is coder

I saw this code snippet from a online tutorial, but I wonder whats the purpose for this assignment:

Programmer.prototype.constructor = Programmer;

Ive tried delete this assignment, and everything works fine.

Qing
  • 1,401
  • 2
  • 21
  • 41
  • This will help: http://stackoverflow.com/questions/18053889/confused-about-douglas-crockfords-object-function – benhowdle89 Nov 24 '14 at 09:51
  • Looks like that tutorial doesn't know how to properly set prototype for inheritance, creating an instance of Parent to set as a prototype of Child shows a lack of understanding of the role the constructor function and the prototype play. The following answer may be helpful to you: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Nov 24 '14 at 13:50

2 Answers2

1

The constructor property lives in the prototype object which in turn your object refers. when you modify the prototype object by doing this Programmer.prototype = new Person(); the constructor property stored in the actual prototype gets lost, because you are overwriting the prototype object itself.

so this line Programmer.prototype.constructor = Programmer; makes sure that you point back to the correct constructor(in other words correct parent object from which you created the object).

It has little significance except for some debugging purposes to know about the actual object from which the new object is created. A point to be noted is since its easy for someone to overwrite to

Programmer.prototype.constructor = Tester;

Extra care should be taken if any of your functionality depends on the constructor property to identify the actual parent, as its not fool proof.

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
1

There is very little difference. The only thing of note is that quite often, type checking (something which Javascript isn't great at when you try to simulate OOP) is done by checking the .constructor property. You test if (someone.constructor === Programmer).

If you remove the Programmer.prototype.constructor = Programmer; line, then someone.constructor is actually Person, so when you test to see if they're a Programmer, it'll fail.

Assigning the Programmer function back as the .constructor method brings that functionality back for you.

To see for yourself, just add console.log( someone.constructor === Programmer, someone.constructor === Person ); to the end of your script, then try removing and adding that one line.

Joe
  • 15,669
  • 4
  • 48
  • 83