This article mainly introduces two main inheritance methods of JavaScript, one is through the prototype, and the other is through the call & amp; apply constructor method, if you are interested, you can refer to the two main methods of js inheritance. one is the prototype method and the other is the call & apply constructor method.
1. prototype ):
Function Body (name, age) {// Create a Body class this. name = name; // assign basic attributes name and age this. age = age;} Body. prototype. sayName = function () {// define a sayName method console for the prototype. log (this. name);} var a = new Body ('wutao', '10'); // Create an instance object function Another () {} Another. prototype = new Body ('WWW '); // you can assign the Body instance object to the prototype attribute of the newly created subclass (Another, another has the attributes of the Body and the method var B = new Another (); // Create an instance of the Another subclass. prototype. sex = "mail"; // defines attributes and methods of sub-classes Another. prototype. saySex = function () {console. log (this. sex);}. sayName (); // wutaob. sayName (); // method sayNameb for www instance B to own the parent class Body. saySex (); // mail instance B has its own defined method saySex
2. borrow the constructor (call & apply). It can also be understood as a combined inheritance.
Call:
Function Person (name) {this. name = name; this. sayHello = function () {console. log (this. name) ;}} function Son (name, age) {Person. call (this, name, age); // call usage: point this pointer to the parent class constructor, and input parameters in sequence so that it has the attributes and methods of the parent class this. age = age; this. sayFunc = function () {console. log (this. name + "-" + this. age) ;}} var a = new Person ('wutao'); var B = new Son ("wwwwww", 22);. sayHello (); // wutaob. sayHello (); // wwwwww; Method of the parent class Person inherited by call sayHellob. sayFunc (); // wwwwww-22
Apply:
Function Person (name) {this. name = name; this. sayHello = function () {console. log (this. name) ;}} function Son (name, age) {Person. apply (this, [name, age]); // apply usage: Similar to call, point this pointer to the parent class constructor and input an array parameter consisting of parameters, make it have attributes and methods of the parent class this. age = age; this. sayFunc = function () {console. log (this. name + "-" + this. age) ;}} var a = new Person ('wutao'); var B = new Son ("ttt", 222);. sayHello (); // wutaob. sayHello (); // ttt; the method sayHellob of the parent class Person inherited by apply. sayFunc (); // ttt-222
The main inheritance methods of js are these two types. of course, there are several inheritance methods, but some inheritance methods after the instance is created, modifying the methods and attributes of an instance directly modifies the methods and attributes of the prototype. this inheritance is meaningless unless the business has similar requirements.
The above is a detailed introduction to the JavaScript inheritance method, and I hope it will help you learn it.