Javascript transfer inheritance (1) _ javascript skills

Source: Internet
Author: User
I have been learning Javascript recently. I plan to write some articles and write my own learning experience. It can be considered a learning note. I don't want to write anything without system knowledge. I mainly want to write something that I think is valuable. Is Javascript an object-oriented language or an object-supporting language, I think everyone has their own opinions. Fans who are loyal to Javascript must say that Javascript is an object-oriented language. For example, Javascript in The Return of the King of Javascript is a prototype-based object-oriented language. Let me talk about my personal views. Although the implementation of object-oriented features, such as inheritance, polymorphism, encapsulation, and Javascript, is not as fast as object-oriented languages such as Java and C #, it also has some support. Therefore, Javascript is an object-oriented language, which has some truth. However, from the perspective of inheritance, there are a series of inheritance methods, but each inheritance method cannot realize the power of true object-oriented language, therefore, object orientation is far-fetched. In summary, my understanding of Javascript is more like calling it a simplified object-oriented approach, or a "pseudo" Object-Oriented Approach (this pseudo word is not derogatory ).

Today we will talk about the first feature of object-oriented: inheritance.
What is inheritance? I don't want to talk nonsense about this. There is an animal, a person, and a girl. This is the simplest and typical inheritance chain.
It is easy to use C # And Other object-oriented methods.

The Code is as follows:


Class Animal
{}
Class People: Animal
{}
Class Girl: People
{}

So in Javascript, there is no class and no inheritance provides implementation. What should we do?
Object disguise (Construction Inheritance Method)
What is object disguise? We may call constructor inheritance easier to understand. As the name suggests, it is to use constructors to play inheritance. In fact, the constructor of the parent class is treated as a common method and put into the constructor of the subclass for execution. In this way, when constructing an object, of course, the subclass object can construct the parent class method!

The Code is as follows:

The Code is as follows:


Function Animal ()
{
This. Run = function () {alert ("I can run ");};
}
Function People (name)
{
// Here the constructor of the parent class is passed in, and then the constructor of the parent class is executed. At this time, the methods in the parent class can be used.
This. father = Animal;
This. father ();
// Remember to delete it. Otherwise, when the subclass is added to a method with the same name as the parent class, it is changed to the parent class.
Delete this. Father;
This. name = name;
This. Say = function () {alert ("My name is" + this. name );}
}
Function Girl (name, age)
{
This. father = People;
This. father (name );
Delete this. father;
This. age = age;
This. Introduce = function () {alert ("My name is" + this. name + ". I am" + this. age );};
}

In this way, an inheritance chain is implemented, and the test is as follows:

The Code is as follows:


Var a = new Animal ();
A. Run ();
Var p = new People ("Windking ");
P. Run ();
P. Say ();
Var g = new Girl ("Xuan", 22 );
G. Run ();
G. Say ();
G. Introduce ();

The result is as follows:

A.

B.

C.

D.

E.

F.

Test successful!

Let's summarize the key of this Code, specifying the parent class, declaring the parent class object, and then deleting the temporary variable. Do you think it is a little troublesome? At least I think so. Once I forget delete, I still have to bear the risk of the parent class being modified. To solve this problem, we use call and apply to improve it!
Next, let's look at the code and the above example (to make it easier for everyone to understand and change the requirement, Animal has a name ):

The Code is as follows:


Function Animal (name)
{
This. Run = function () {alert ("I can Run ");};
}
Function People (name)
{
// Use the call method to implement inheritance
This. father = Animal;
This. father. call (this, name );
This. name = name;
This. SayName = function () {alert ("My name is" + this. name ;);};
}
Function Girl (name, age)
{
// Use the apply method to implement inheritance
This. father = People;
This. father. apply (this, new Array (name ));
This. age = age;
This. Introduce = function () {alert ("My name is" + this. name + ". I am" + this. age );};
}

With the same test code, we found that the test was as successful.

If you are a newbie, you may be confused about the two sections of code. What is call and what is apply? Well, I joined a series of additional journals in this topic. If you don't know about this, read my article: call and apply.
Object disguise is just a way to implement inheritance. In the following article, I will continue to write other inheritance methods and the advantages and disadvantages of several inheritance methods.

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.