I am learning JavaScript and found two ways of assigning prototype.
The first is A.prototype = B.prototype
and the second is A.prototype = new B()
For example:
function A() {
console.log("A!")
}
function B() {
console.log("B!")
}
// First case
A.prototype = B.prototype;
a = new A(); // a instanceof A,B
// Second case
A.prototype = new B();
a = new A(); // a instanceof A,B
- Is there any difference and which way to prefer?
- Is there any other way to assign prototype?
Update:
As Felix Kling advised there is a third way to assign a prototype:
A.prototype = Object.create(B.prototype);