JS inheritance note _ js object-oriented-js tutorial

Source: Internet
Author: User
JavaScript does not have the concept of classes, and class-related inheritance is even more difficult to talk about. However, we can use special syntax to simulate inheritance in object-oriented languages. JS inheritance
JavaScript does not have the concept of classes, and the concept of class-related inheritance is even more difficult to talk about, but we can use special syntax

Simulate inheritance in the object-oriented language.
There are multiple methods to simulate inheritance in JS. The parasitic combination mode is a simple simulation inheritance mode. Let's take a look at the following:

This article describes how to simulate inheritance in the parasitic combination mode.
JS inheritance includes property inheritance and method inheritance, which are implemented through different methods.
1. property inheritance
Property inheritance is implemented by changing the execution environment of the function. The call () and apply () methods can be used to change the execution environment of the function.

Method.
We first create an Animal "class" (because JS does not have the class concept, it is just a simulation, it is actually just

Function object ).

The Code is as follows:


Function Animal (name ){
This. name = name;
}


Create another Lion "class" and "inherit" from Animal

The Code is as follows:


Function Lion (){
Animal. apply (this, ["lion"]);
}


Here, the apply method of Animal is used to change the execution environment of Animal to the execution environment when Lion is called.
To use the Lion class, we usually need a new Lion. For example:
Var l = new Lion ();
The new keyword is great. In the previous code, the new Keyword completes the following tasks:
1) Open up heap space to prepare to store Lion objects
2) modify the execution environment of the Lion object so that this of the Lion function points to the Lion function object.
3) call the "Constructor" of the Lion "class" to create a Lion object.
4) Assign the heap address of the Lion function object to variable l. At this time, l points to the Lion function object.
So after the new Keyword, this in Animal. apply (this, [""]) has pointed to the Lion function object

In this Code, the execution environment of the Animal function is changed to the Lion function, which is equivalent to the following code:

The Code is as follows:


Function Lion (){
Function Animal (name ){
This. name = name;
}
}


At this time, this is already the Lion function object, so the code above is further equivalent:

The Code is as follows:


Function Lion (){
This. name = name;
}


This adds the name attribute to the Lion function object and simulates the effect that the Lion function inherits from the Animal function.
2. Method inheritance
In JS, each "class" (that is, a function, note that it is not a function object) has a prototype attribute. prototype indicates the function.

It also represents a set of class members (usually a set of methods ). We can implement this by using the prototype attribute of the function.

Method.
We also create an Animal "class" first ":

The Code is as follows:


Function Animal (name ){
This. name = name;
}


Add an eat Method to the Animal prototype:

The Code is as follows:


Animal. prototype. eat = function (){
Alter ("I can eat !~ ");
}


Create a Lion "class" and inherit the attributes of the Animal "class"

The Code is as follows:


Function Lion (){
Animal. apply (this, ["lion"]);
}


Pay attention to the following code. We are about to inherit the method.
Lion. prototype = new Animal ();
In this way, an Animal function object is stored in the Lion prototype, and Lion contains the methods in Animal (in fact

Including attributes ). In this way, the Lion function inherits Animal.
Related Article

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.