0

I have a prototypal inheritance like below where Student extends Guru. I have three questions, can someone clarify the same.

function Guru(name){
  this.name = name;
}

Guru.prototype.copy = function(){
  return this.constructor(this.name);
}

function Student(name){
  Guru.call(this)
  this.name = name;
}

Student.prototype = Object.create(Guru.prototype);
Student.prototype.constructor = Student;
var stu = new Student("John Cena");

console.log(stu.constructor);
console.log(stu.__proto__);
  1. Why should we avoid Student.prototype = new Guru();
  2. What is the difference between these two:

    console.log(stu.constructor);
    console.log(stu.__proto__);
    

    Prints the below:

    [Function: Guru]
    Guru { constructor: [Function: Student] }
    
  3. Difference between constructor.prototype and prototype.constructor? Do we have constructor.prototype in javascript?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
ShankarGuru
  • 627
  • 1
  • 6
  • 17
  • Some brilliant answers here http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work, especially the second answer. I'd also recommand reading an article about programming paradigms. – Noctisdark Feb 20 '16 at 21:49
  • Please ask only one question per post. – Bergi Feb 20 '16 at 22:51
  • Btw, the first question is answered [here](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) and [there](http://stackoverflow.com/a/17393153/1048572?Benefits-of-using-Object.create-for-inheritance) – Bergi Feb 20 '16 at 22:52
  • All your students are gurus? Wow. Btw, that `copy` method misses a `new`. – Bergi Feb 20 '16 at 22:55

1 Answers1

1
  1. Why should we avoid Student.prototype = new Guru()?

Because Guru constructor expects an instance, not a subclass. Properties created in that constructor should be assigned directly to the instance.

In your case it doesn't matter much, but imagine this:

function C1() { this.o = []; }
function C2() {}
C2.prototype = new C1();
var c1a = new C1(), c1b = new C1(),
    c2a = new C2(), c2b = new C2();
c1a.o.push(123);
c2a.o.push(456);
c1b.o; // []    -- The property is NOT shared among C1 instances
c2b.o; // [456] -- The property is shared among all Sub instances
  1. What is the difference between stu.constructor and stu.__proto__?

When you create the Student constructor, it receives automatically a prototype with a constructor property which points back to Student.

Instead , __proto__ is a getter which returns the [[Prototype]] of an object. Note this is not much standard (it's only defined in an annex for browsers), you should use Object.getPrototypeOf instead.

Therefore, stu.constructor (inherited from Student.prototype) is Student. And stu.__proto__ (inherited from Object.prototype) is Student.prototype.

  1. Difference between constructor.prototype and prototype.constructor?

Using constructor.prototype on a prototype is pointless because it gives the same prototype (assuming it has not been altered).

Using constructor.prototype on a instance gives the prototype it inherits from (assuming it is not shadowed nor has been altered).

Using prototype.constructor on a constructor is pointless because it gives the same constructor (assuming it has not been altered).

Oriol
  • 274,082
  • 63
  • 437
  • 513