Inheritance of the prototype chain
/** * Core: uses the instance of the parent class as the prototype of the subclass * Advantages: Method reuse * Since the method is defined on the prototype of the parent class, it multiplies the constructor methods of the parent class, such as the say method * Disadvantages: * When an instance of a subclass is created, it cannot pass arguments to its parent class. * When an instance of a subclass shares a reference property to the constructor of its parent class, Attributes such as arr * unable to implement multiple inheritance * * * * / function Parent (name) {enclosing name = name | | 'Parent / / instance basic attribute (the attribute emphasizes private, } parent.prototype. say = function (){// Define the method on the prototype object (Emphasis on reuse, } function Child(like){this.like = like} child. prototype = new Parent() // Core, Child at this time. The prototype. The constructor = = = the Parent console. The log (Child) prototype) constructor = = = the Parent) / / true / / A complete prototype object must have a constructor Child. The prototype. The constructor = Child / / modify the constructor to let boy1 = new Child () let boy2 = new Child() // Hello console.log(boy1.say()) // hello console.log(boy2.say())) // hello console.log(boy1.say() === boy2.say())) // true // shortcoming 1 cannot pass the argument console.log(boy1.name) // parent console.log(boy2.name) // parent console.log(boy1.name === boy2.name) // // Arr boy1.arr.push(2) // Modify the arr of boy1, affecting the arr console.log(boy2.arr) // [1,2]