Analysis of Two JavaScript inheritance methods-javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.