JavaScript implements a hybrid method of inheritance, and javascript inherits

Source: Internet
Author: User

JavaScript implements a hybrid method of inheritance, and javascript inherits

The simplest way to implement JavaScript inheritance is the call method (or the apply method) and the prototype chain method. However, both methods have defects, and their mixture is a good inheritance implementation method. The following is an example:

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

Animal. prototype. sayAge = function (){
Window. alert ("My age is" + this. age + "! ");
};

Function Dog (age, name ){
Animal. call (this, age );
This. name = name;
}
Dog. prototype = new Animal ();
Dog. prototype. sayName = function (){
Window. alert ("I am a" + this. name + "! ");
};

Var dog = new Dog (15, "dog ");
Dog. sayName ();
Dog. sayAge ();

For the class Animal, it has a field attribute age and function attribute sayAge. The definition of the sayAge method uses the prototype method. The Dog class must inherit Animal, and its field attributes have name besides age, which can be inherited and initialized by Dog through Animal. call (this, age. The first parameter of the call method is the this pointer of the inherited class, and the second parameter is the parameter of the constructor of the Animal class. In fact, only the call method can be used to implement inheritance, but the only requirement is that the function attribute of the parent class should be defined in the constructor, this is not suitable for defining function properties using the prototype (defining function properties using the prototype is more intuitive than defining function properties within the constructor ). To inherit the function attributes defined by the Animal prototype, the required statement is "Dog. prototype = new Animal ();". The sayName () function in the Dog class is its own function attribute.
In addition to the most classic method of implementation inheritance, there are currently some free libraries available. However, if you think of a variety of libraries, the header will be big. If you have time to study it again!


How does one implement Java class inheritance in JavaScript?

Js inheritance has five implementation methods: 1. Inheritance first: the object impersonates function Parent (username) {this. username = username; this. hello = function () {alert (this. username) ;}} function Child (username, password) {// append the Parent attributes and methods to the Child using the following three rows, so as to inherit from the Child: // Step 1: this. method is a temporary property and points to the object pointed to by the Parent. // Step 2: execute this. method, that is, execute the object function pointed to by Parent // Step 3: destroy this. method attribute, that is, Child has all the attributes and methods of the Parent. this. method = Parent; this. method (username); // The most critical row to delete this. method; this. password = password; this. world = function () {alert (this. password) ;}} var parent = new Parent ("zhangsan"); var child = new Child ("lisi", "123456"); parent. hello (); child. hello (); child. world (); 2. inherit the second method: call () method call method is the value of the first parameter of the method call method in the Function class assigned to the class (that is, the method) the second parameter of this call method that appears in starts to be assigned to the function test (str) {alert (this. name + "" + str);} var object = new Object (); object. name = "zhangsan"; test. call (object, "langsin"); // at this time, the first parameter value object is passed to this, the second parameter "langsin" is assigned to str function Parent (username) {this. username = username; this. hello = function () {alert (this. username) ;}} function Child (username, password) {Parent. call (this, username); this. password = password; this. world = function () {alert (this. password) ;}} var parent = new Parent ("zhangsan"); va ...... remaining full text>

What are the two types of javascript inheritance?

In many books, the inheritance of Javascript is divided into many types and implementation methods, which are basically two types: Object impersonating and prototype. The two methods have their own advantages and disadvantages. Here I will list them for you, and then analyze the differences from the underlying layer:

(1) object impersonating

Function A (name ){
This. name = name;
This. sayHello = function () {alert (this. name + "say Hello !");};
}
Function B (name, id ){
This. temp =;
This. temp (name); // equivalent to new ();
Delete this. temp; // prevent overwrite attributes and methods of super class A through temp reference later
This. id = id;
This. checkId = function (ID) {alert (this. id = ID )};
}

When constructing object B, calling temp is equivalent to starting the constructor of A. Note that this object in the context environment is an instance of B, so when executing the constructor script, all the variables and methods of A are assigned to the object referred to by this, that is, the instance of B, so that B inherits the attribute method of. Then, delete the temporary reference temp to prevent the maintenance of Class Object A (not an instance object) Reference changes in Class B, because changing temp will directly lead to changes in the structure of Class A (note that it is not the object of Class.

We can see that in the Js version update process, the call and apply functions are added to facilitate the execution of this context switch to achieve inheritance or a broader sense. They work in the same way, except for different versions of parameters (a variable of any parameter, an array must be input as a parameter set ). Here we take the call as an example to explain how to pretend to inherit an object implemented by call.

Function Rect (width, height ){
This. width = width;
This. height = height;
This. area = function () {return this. width * this. height ;};
}
Function myRect (width, height, name ){
Rect. call (this, width, height );
This. name = name;
This. show = function (){
Alert (this. name + "with area:" + this. area ());
}
}

For the Call method, the official explanation is: Call a method of an object to replace the current object with another object.
Call (thisOb, arg1, arg2 ...)

This is also an inheritance of object impersonating. In fact, what happens when the call method is called is also the replacement of the context variable this, in the myRect function body, this must point to an instance of the class myRect object. However, this is used as the context variable to call the Rect method, that is, the constructor of the class Rect. Therefore, when calling Rect, the attributes and methods of assigning this values are actually for a myRect object. So although call and apply are not just new methods for inheritance, they can be used to simulate inheritance.

This is the same thing for object impersonating inheritance. It can implement multiple inheritance, as long as you repeat the assignment process. However, it is not widely used at present. Why? Because it has an obvious performance defect, we need to talk about the concept of OO. We say that an object is a set of member + member methods. When constructing an object instance, these instances only need to have their own member variables ...... remaining full text>

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.