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__);
- Why should we avoid
Student.prototype = new Guru();
What is the difference between these two:
console.log(stu.constructor); console.log(stu.__proto__);
Prints the below:
[Function: Guru] Guru { constructor: [Function: Student] }
Difference between
constructor.prototype
andprototype.constructor
? Do we have constructor.prototype in javascript?