1. Prototype chain inheritancefunction Parent () { this.name = 'kevin'; } Parent.prototype.getName = function () { console.log(this.name); } function Child () { } Child.prototype = new Parent(); var child1 = new Child(); console.log(child1.getName()) // kevin question: 1. The properties of reference types are shared by all instances. For example: function Parent () { this.names = ['kevin', 'daisy']; } function Child () { } Child.prototype = new Parent(); var child1 = new Child(); child1.names.push('yayu'); console.log(child1.names); // ["kevin", "daisy", "yayu"] var child2 = new Child(); console.log(child2.names); // ["kevin", "daisy", "yayu"] 2. When creating an instance of Child, you cannot pass parameters to Parent 2. Borrowing constructors (classical inheritance)function Parent () { this.names = ['kevin', 'daisy']; } function Child () { Parent.call(this); } var child1 = new Child(); child1.names.push('yayu'); console.log(child1.names); // ["kevin", "daisy", "yayu"] var child2 = new Child(); console.log(child2.names); // ["kevin", "daisy"] advantage:
For example: function Parent (name) { this.name = name; } function Child (name) { Parent.call(this, name); } var child1 = new Child('kevin'); console.log(child1.name); // kevin var child2 = new Child('daisy'); console.log(child2.name); // daisy shortcoming:
3. Combination inheritancePrototype inheritance and classical inheritance work together. function Parent (name) { this.name = name; this.colors = ['red', 'blue', 'green']; } Parent.prototype.getName = function () { console.log(this.name) } function Child (name, age) { Parent.call(this, name); this.age = age; } Child.prototype = new Parent(); var child1 = new Child('kevin', '18'); child1.colors.push('black'); console.log(child1.name); // kevin console.log(child1.age); // 18 console.log(child1.colors); // ["red", "blue", "green", "black"] var child2 = new Child('daisy', '20'); console.log(child2.name); // daisy console.log(child2.age); // 20 console.log(child2.colors); // ["red", "blue", "green"] Advantages: It combines the advantages of prototype chain inheritance and constructor function and is the most commonly used inheritance mode in JavaScript. 4. Prototype inheritancefunction createObj(o) { function F(){} F.prototype = o; return new F(); } It is a simulated implementation of Disadvantages: Property values containing reference types always share the corresponding values, just like prototype chain inheritance. var person = { name: 'kevin', friends: ['daisy', 'kelly'] } var person1 = createObj(person); var person2 = createObj(person); person1.name = 'person1'; console.log(person2.name); // kevin person1.firends.push('taylor'); console.log(person2.friends); // ["daisy", "kelly", "taylor"]
5. Parasitic inheritanceCreate a function that is used only to encapsulate the inheritance process, which internally enhances the object in some form and finally returns the object. function createObj (o) { var clone = object.create(o); clone.sayName = function () { console.log('hi'); } return clone; } Disadvantages: Like the borrowed constructor pattern, a method is created each time an object is created. 6. Parasitic Combinatorial InheritanceFor your convenience, let's repeat the combined inheritance code here: function Parent (name) { this.name = name; this.colors = ['red', 'blue', 'green']; } Parent.prototype.getName = function () { console.log(this.name) } function Child (name, age) { Parent.call(this, name); this.age = age; } Child.prototype = new Parent(); var child1 = new Child('kevin', '18'); console.log(child1) The biggest disadvantage of composite inheritance is that the parent constructor is called twice. Once is when setting the prototype of a subtype instance: Child.prototype = new Parent(); Once when creating an instance of the subtype: var child1 = new Child('kevin', '18'); Recall the simulation implementation of new. In fact, in this sentence, we will execute: Parent.call(this, name); Here, we call the Parent constructor again. So, in this example, if we print the So how can we strive for excellence and avoid repeated calls this time? What if we don’t use See how it is implemented: function Parent (name) { this.name = name; this.colors = ['red', 'blue', 'green']; } Parent.prototype.getName = function () { console.log(this.name) } function Child (name, age) { Parent.call(this, name); this.age = age; } // Three key steps var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); var child1 = new Child('kevin', '18'); console.log(child1); Finally, we encapsulate this inheritance method: function object(o) { function F() {} F.prototype = o; return new F(); } function prototype(child, parent) { var prototype = object(parent.prototype); prototype.constructor = child; child.prototype = prototype; } // When we use: prototype(Child, Parent); Quoting the praise of parasitic combinatorial inheritance in "Advanced JavaScript Programming": The efficiency of this approach is that it only calls the This concludes this article on in-depth explanation of the various ways of inheritance in JavaScript and their advantages and disadvantages. For more information on various ways of inheritance in JavaScript and their advantages and disadvantages, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Detailed explanation of character sets and validation rules in MySQL
MySQL Limit can query database data in segments a...
mysql full backup 1. Enable binary log and separa...
Now many mobile phones have the function of switc...
The javascript function for converting <table&g...
Recently, when developing a small program, I enco...
Note: Since .NET FrameWork cannot be run in core ...
Table of contents 1. Principle of animation funct...
Before learning awk, we should have learned sed, ...
1. Download from the official website and unzip h...
The usage format of the mysqladmin tool is: mysql...
need: The official website's resource server ...
Today is 618, and all major shopping malls are ho...
Table of contents Preface 1. Install scp2 2. Conf...
Table of contents What is a container data volume...
This article example shares the specific code of ...