// JS inheritance most common - in combination inheritance function Super {(name) the this .name = name; the this .colors = [ "Red", "Blue", "Green" ]; } Super.prototype.sayName = function(){ console.log(this.name); } function Sub (name) { // inherits the properties Super.call ( the this , name); } // inherits a method Sub.prototype = new new Super (); var sub1 = new new Sub ( "Nick" ); sub1.colors.push("black"); console.log(sub1.colors); //(4) ["red", "blue", "green", "black"] sub1.sayName(); //Nick var sub2 = new Sub("Lucy"); console.log(sub2.colors); //(3) ["red", "blue", "green"] sub2.sayName(); //Lucy
Combination of inherited and avoids borrowing constructor prototype chain defects, combines their advantages, js become the most commonly used mode of inheritance.
// returns are to true console.log (sub1 instanceof Object); console.log(sub1 instanceof Super); console.log(sub1 instanceof Sub); console.log(Object.prototype.isPrototypeOf(sub2)); console.log(Super.prototype.isPrototypeOf(sub2)); console.log(Sub.prototype.isPrototypeOf(sub2));
And instanceof and isPrototypeOf () method is also able to correctly identify the object based on a combination of inheritance created.