The previous sections talk about JavaScript-oriented namespaces, JavaScript-oriented JavaScript classes, private members of JavaScript-oriented objects, and public members and JavaScript-oriented overloading, so you can look at the above and continue down See.
There are several ways to implement inheritance in JavaScript, and here are two common types.
One, call inheritance, first look at the code:
First define a "people" class
Copy Code code as follows:
Human
Person=function () {
This.name= "Grass mud Horse";
This.eat=function () {
Alert ("I Want to eat");
}
This.sleep=function () {
Alert ("I Want to Sleep");
}
}
Then define a student class and let it inherit from person
Copy Code code as follows:
Student class
Student=function () {
Person.call (this);//Inheriting Person class
This.dohomework=function () {
Alert ("The teacher came, the homework to borrow me to copy");
}
}
The key to see Person.call (this), where this represents the current object, that is, student, is easy to understand, and person.call (this) means that all exposed members of the person class are "appended" to the student class, Let student also have all the features of person.
As with high-level languages, if a member with the same name as the parent class appears in the subclass, it is overwritten, which is called "rewrite."
Again, we define a girl class:
Copy Code code as follows:
Girls ' class
Girl=function () {
Person.call (this);//Inheriting Person class
this.sex= "female";
}
JavaScript is able to implement multiple inheritance, see the following Master (Master) class, this master is a student, but also a beautiful mm, so the following code:
Copy Code code as follows:
Master Class
Master=function () {
Student.call (this);//Inheriting Student class
Girl.call (this);//Inheriting Girl class
This.degree= "master";/education
}
We can test:
Copy Code code as follows:
var master=new master ();
Master.name= "Sister Feng";
Master.eat ();
Master.dohomework ();
Alert ("Occasionally is the name:" +master.name);
Alert ("Even sex is:" +master.sex);
In order to be aware of multiple inheritance, if two classes have members of the same name, the latter will overwrite the previous one, that is, the current class will only inherit members from the latter class.
Call method Inheritance on this, if you are not clear about the method, please go to ask Google brother, I will not repeat, the online reprint not too much. Here is another way to inherit.
Second, prototype prototype inheritance:
We define another class of monitor:
Copy Code code as follows:
Class Monitor
Squadleader=function () {
Say hello
This.hi=function () {
Alert ("Good class, I am the Monitor Now");
}
}
Above defines a master class, now this master is promoted to monitor, so this master to inherit Squadleader, this time we use prototype to realize, please see the following code:
Copy Code code as follows:
Master.prototype=new Squadleader ();//prototype attribute points to an object
Or
Master.prototype=squadleader.prototype;
So master inherits the Squadleader class, so one word, here are two forms, in fact, the principle is the same. It means: Copy Squadleader's "Soul" to master, so that master can do what Squadleader can do.
Test:
var master=new master ();
Master.hi ()/output "Good class, I am now the monitor of the"
I personally prefer to use the first scheme (call inheritance) to implement inheritance, all the code is wrapped in a "{}", at a glance, in the writing style than the second closer to C #. Prototype properties I am generally used to extend an existing class.
JavaScript is a very flexible door language, to achieve inheritance there may be other better way, we can go to research and excavation, I am in this as a throw a brick, hoping to lead to the gorgeous jade!