There are many ways to implement inheritance in JavaScript, and the following are two common ones.
1. Call inheritance, look at the code first:
Define a "human" class first
//Human
Person=function(){
="Blooming light";
=function(){
alert("I want to have a meal");
}
=function(){
alert("I want to sleep");
}
}
Define a student class and let it inherit from Person
//Students
Student=function(){
(this);//Inherit the Person class
=function(){
alert("The teacher is here, I will borrow my homework to copy");
}
}
The key is to look at the sentence (this), where this represents the current object, namely Student, which is easy to understand, and (this) means: "attach" all public members of the Person class to the Student class, so that Student also has all the functions of Person.
Like high-level languages, if a member of the same name as the parent class appears in a subclass, it will be overwritten, which is the so-called "rewrite".
Similarly, let's define another girl class:
//Girls
Girl=function(){
(this);//Inherit the Person class
="Female";
}
JavaScript can achieve multiple inheritance. Please see the Master (Master) class below. This Master is naturally a student, but at the same time he is a beautiful MM, so the following code is available:
//Master's
Master=function(){
(this);//Inherit the Student class
(this);//Inherit the Girl class
="Master's degree";//Education
}
We can test it:
var master=new Master();
="Rururu";
();
();
alert("I am the name: "+);
alert("The gender of an hour is:"+);
Pay attention to the order when inheriting multiple times. If there are members of the same name in the two classes, the latter will override the previous one, which means that the current class will only inherit members of the next class.
That’s all about the inheritance of the call method. If you don’t know the call method, please go and ask Brother Google. I won’t go into details. Don’t reprint it too much online. Let’s talk about another inheritance method below.
2. Prototype prototype inheritance:
Let's define another class leader class:
//Squad Leader Class
SquadLeader=function (){
//greet
=function(){
alert("Hello, classmates, I am the class monitor now");
}
}
A master's class is defined above. Now this master has been promoted to class leader, so this master has to inherit SquadLeader. This time we use prototype to implement it. Please see the following code:
=new SquadLeader();//prototype attribute points to an object
//or
//=;
In this way, Master inherits the SquadLeader class. In this way, there are two forms here, but the principles are actually the same. It means: copy the "soul" of SquadLeader to Master, so from now on, Master can do everything SquadLeader can do.
Test it:
var master=new Master();
()//Output "Hello, classmates, I am the class monitor now"
I personally prefer to use the first solution (call inheritance) to implement inheritance. All code is included in one "{}", which is clear at a glance, and it is closer to C# in writing style than the second one. I usually use the prototype attribute to extend existing classes.
JavaScript is a very flexible language. There may be other better ways to achieve inheritance. You can study and explore it. I will just treat it as a throwing brick, hoping to lead to a gorgeous jade!
1. Call inheritance, look at the code first:
Define a "human" class first
Copy the codeThe code is as follows:
//Human
Person=function(){
="Blooming light";
=function(){
alert("I want to have a meal");
}
=function(){
alert("I want to sleep");
}
}
Define a student class and let it inherit from Person
Copy the codeThe code is as follows:
//Students
Student=function(){
(this);//Inherit the Person class
=function(){
alert("The teacher is here, I will borrow my homework to copy");
}
}
The key is to look at the sentence (this), where this represents the current object, namely Student, which is easy to understand, and (this) means: "attach" all public members of the Person class to the Student class, so that Student also has all the functions of Person.
Like high-level languages, if a member of the same name as the parent class appears in a subclass, it will be overwritten, which is the so-called "rewrite".
Similarly, let's define another girl class:
Copy the codeThe code is as follows:
//Girls
Girl=function(){
(this);//Inherit the Person class
="Female";
}
JavaScript can achieve multiple inheritance. Please see the Master (Master) class below. This Master is naturally a student, but at the same time he is a beautiful MM, so the following code is available:
//Master's
Master=function(){
(this);//Inherit the Student class
(this);//Inherit the Girl class
="Master's degree";//Education
}
We can test it:
Copy the codeThe code is as follows:
var master=new Master();
="Rururu";
();
();
alert("I am the name: "+);
alert("The gender of an hour is:"+);
Pay attention to the order when inheriting multiple times. If there are members of the same name in the two classes, the latter will override the previous one, which means that the current class will only inherit members of the next class.
That’s all about the inheritance of the call method. If you don’t know the call method, please go and ask Brother Google. I won’t go into details. Don’t reprint it too much online. Let’s talk about another inheritance method below.
2. Prototype prototype inheritance:
Let's define another class leader class:
Copy the codeThe code is as follows:
//Squad Leader Class
SquadLeader=function (){
//greet
=function(){
alert("Hello, classmates, I am the class monitor now");
}
}
A master's class is defined above. Now this master has been promoted to class leader, so this master has to inherit SquadLeader. This time we use prototype to implement it. Please see the following code:
=new SquadLeader();//prototype attribute points to an object
//or
//=;
In this way, Master inherits the SquadLeader class. In this way, there are two forms here, but the principles are actually the same. It means: copy the "soul" of SquadLeader to Master, so from now on, Master can do everything SquadLeader can do.
Test it:
var master=new Master();
()//Output "Hello, classmates, I am the class monitor now"
I personally prefer to use the first solution (call inheritance) to implement inheritance. All code is included in one "{}", which is clear at a glance, and it is closer to C# in writing style than the second one. I usually use the prototype attribute to extend existing classes.
JavaScript is a very flexible language. There may be other better ways to achieve inheritance. You can study and explore it. I will just treat it as a throwing brick, hoping to lead to a gorgeous jade!