JavaScript Play Inheritance (i) _javascript tips

Source: Internet
Author: User
Tags inheritance

I think everyone has their own views on whether JavaScript is an object-oriented language or a language that supports objects. Those JavaScript-faithful fans must have said that JavaScript is an object-oriented language, and that JavaScript in the book "The Return of the JavaScript King" is based on a prototype object oriented approach. I'd like to talk about my personal opinion. Object-oriented three features, inheritance, polymorphism, encapsulation, JavaScript, although not implemented like java,c#, such as object-oriented language faster, but after all, there is some support. Therefore, it is reasonable to say that JavaScript is an object-oriented language, but from the inheritance of this part to talk about a series of inheritance, but each inheritance law can not realize the power of real object-oriented language, therefore, said he object-oriented has a certain far-fetched. In summary, my understanding of JavaScript is more like to call it a simplified object-oriented, or "pseudo" object oriented (this pseudo word has no derogatory meaning).

Today we'll talk about the first feature of object-oriented: inheritance.
What is inheritance? This I do not want nonsense, there is an animal, there is a person, there is a girl, this is one of the simplest, is also a typical inheritance chain.
In C # and other object-oriented objects, it's easy.

Copy Code code as follows:

Class Animal
{    }
Class People:animal
{    }
Class Girl:people
{    }

So in JavaScript, there are no classes, no inherited delivery implementations, what do we do?
Object Camouflage (Structural inheritance method)
What is an object disguise? We may be called structural inheritance easier to understand some. As the name implies, is to play inheritance with the constructor. In fact, it is said that the constructor of the parent class as a common method, put into the constructor of the subclass to execute, so that, when the object is constructed, the subclass of course can construct the parent class Method!

Or use the example above, the code is as follows:

Copy Code code as follows:

function Animal ()
{
This. Run=function () {alert ("I can Run");};
}
function people (name)
{
This is where the parent class constructor is passed in, and then the parent class is constructed, at which point the method in the parent class can be used.
This.father=animal;
This.father ();
Remember to delete, otherwise the subclass is modified to the parent class when it is added to a method with the same name as 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 tested:

Copy Code code 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 results are as follows:

A.

B.

C.

D.

E.

F.

Test success!

Let's summarize the key to this code, specify the parent class, declare the parent class object, and then delete the temporary variable, do you feel a bit of a problem? At least I think so, once forgotten delete, but also bear the risk of being modified by the parent class, for this, we use call and apply to improve!
Then look at the code, or the example above (in order to make it easier for everyone to understand, the need to change, animal has a name):

Copy Code code as follows:

function Animal (name)
{
This. Run=function () {alert ("I can Run");};
}
function people (name)
{
Using 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)
{
Using 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, found the test as successful.

If it is a novice, may look at the back of the two pieces of code some dizzy, what is call, what is apply it? Well, in the topic of play-and-inherit, I add a supplement series, and if I don't know about it, I can read this article: "Topsy: Call and apply."
Object camouflage, this is just a way to implement inheritance, in the next article, I will continue to write other ways of inheritance and several ways to inherit the pros and cons, welcome to continue to focus on.

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.