Javascript object-oriented prototype inheritance of new rational training _ js object-oriented

Source: Internet
Author: User
There are two key steps to inherit from the prototype. For more information, see. First, create an instantiated object of the parent class, and then assign the object to the prototype attribute of the subclass.
In this way, all public instance members in the parent class will inherit from the quilt class. When the instanceof operator is used to determine whether the instantiated object of the subclass belongs to both the subclass and the parent class.

Then assign the subclass itself to its prototype constructor attribute. (Note: when assigning values here, there is no !)
This step ensures that the subclass definition is displayed when you view the constructor attribute of the instantiated object of the subclass, rather than the definition of its parent class.

Next. in the result of the method1 () call, we can see that in the public instance method inherited by the subclass, if the private instance field or private instance method is called, the called Private instance members belong to the parent class.

Similarly. the results of the method2 () call show that if the private instance field or private instance method is called, The called Private instance members belong to the subclass.

Through the result of calling o. method (), we can see that the method defined on the parent class prototype will inherit the quilt class.
The result of calling o. method3 () shows that the instance method defined in the subclass cannot access the private instance members defined in the parent class.
Finally, we can see from the results of the call to subClass. staticMethod () that static members will not be inherited.

2.4 call the Inheritance Law
The essence of calling inheritance is that in the constructor of the subclass, the constructor method of the parent class is executed in the context of the sub-class execution, in the parent constructor method, all the content operated by this method is actually the content on the instantiated object of the subclass operated. Therefore, this approach is only intended to reduce repeated code writing.

The Code is as follows:


Function parentClass (){
// Private field
Var x = "I'm a parentClass field! ";
// Private method
Function method1 (){
Alert (x );
Alert ("I'm a parentClass method! ");
}
// Public field
This. x = "I'm a parentClass object field! ";
// Public method
This. method1 = function (){
Alert (x );
Alert (this. x );
Method1 ();
}
}
ParentClass. prototype. method = function (){
Alert ("I'm a parentClass prototype method! ");
}

ParentClass. staticMethod = function (){
Alert ("I'm a parentClass static method! ");
}

Function subClass (){
// Inherit
ParentClass. call (this );

// Private field
Var x = "I'm a subClass field! ";
// Private method
Function method2 (){
Alert (x );
Alert ("I'm a subClass method! ");
}
// Public field
This. x = "I'm a subClass object field! ";
// Public method
This. method2 = function (){
Alert (x );
Alert (this. x );
Method2 ();
}
This. method3 = function (){
Method1 ();
}
}

// Test
Var o = new subClass ();

Alert (o instanceof parentClass); // false
Alert (o instanceof subClass); // true

Alert (o. constructor); // function subClass (){...}

O. method1 (); // I'm a parentClass field!
// I'm a subClass object field!
// I'm a parentClass field!
// I'm a parentClass method!
O. method2 (); // I'm a subClass field!
// I'm a subClass object field!
// I'm a subClass field!
// I'm a subClass method!

O. method (); // Error !!!
O. method3 (); // Error !!!
SubClass. staticMethod (); // Error !!!


The above example shows how to use the inheritance method to implement inheritance.
The key to inheritance by calling is only one step:
When defining a subclass, The this pointer of the subclass is passed in through the call method of the parent class. Make the parent class method run in the context of the subclass.
In this way, all the public instance members defined in this mode within the parent class will inherit from the quilt class.
When determined by the instanceof operator, the instantiated object of the subclass only belongs to the subclass and does not belong to the parent class.
When viewing the constructor attribute of the instantiated object of the subclass, we can see the definition of the subclass, not the definition of its parent class.
Next, the results of calling o. method1 () and o. method2 () are the same as those of the prototype inheritance method, and the problems described are the same.
Through the result of calling o. method (), we can see that the method defined on the parent class prototype will not inherit from the quilt class.
By calling o. method3 (), we can see that the instance methods defined in the subclass cannot access the private instance members defined in the parent class.
Finally, we can see from the results of the call to subClass. staticMethod () that static members will not be inherited.
Finally, this example does not show that multiple inheritance can be implemented by calling the inheritance law. That is to say, a subclass can inherit from multiple parent classes all the public instance members defined in the parent class by using this method.
As a weak language, javascript provides abundant polymorphism, Which is incomparable to other strong object-oriented languages.
Polymorphism
Heavy Load and coverage
The differences between heavy load and overwrite are described first. Overload is used in English and override is used in English. It is found that most people on the internet use override as a heavy load, which is not correct. There is a difference between heavy load and coverage.
Overload means that a function with the same name (note that this includes a function) or method can have multiple implementations, which are identified by the parameter type and the number of (or) parameters.
Override means that sub-classes can define methods with the same name as the parent class and the same parameter type and number. After the definition of these methods, in the instantiated object of the sub-classes, the methods inherited from the parent class with the same name will be hidden.
Heavy Load
In javascript, function parameters are of no type and the number of parameters is arbitrary. For example, although you can define:

The Code is as follows:


Function add (a, B ){
Return a + B;
}


Such a function, but you can still call it to include any number of parameters. Of course, the parameter type is also arbitrary. As for whether an error occurs, it is determined by the content executed in this function. javascript does not determine which function you call based on the number and type of parameters you specify.
Therefore, to define overload methods, you cannot do the same as in a strongly typed language. However, you can still implement overload. It is through the arguments attribute of the function. For example:

The Code is as follows:


Function add (){
Var sum = 0;
For (var I = 0; I <arguments. length; I ++ ){
Sum + = arguments [I];
}
Return sum;
}


In this way, you can overload any number of parameter addition functions.
Of course, you can also use instanceof or constructor in a function to determine the type of each parameter and decide what operations to perform next to implement more complex functions or method overloading. In short, the javascript overload is implemented by the user by operating the arguments attribute in the function.
Overwrite
It is easy to implement coverage, for example:

The Code is as follows:


Function parentClass (){
This. method = function (){
Alert ("parentClass method ");
}
}
Function subClass (){
This. method = function (){
Alert ("subClass method ");
}
}
SubClass. prototype = new parentClass ();
SubClass. prototype. constructor = subClass;

Var o = new subClass ();
O. method ();


In this way, the method defined in the subclass overwrites the method inherited from the parent class.
You may say that this overwrite method is good, but in java, the overwrite method can call the method to be overwritten (the method of the parent class). How can this be implemented here? It is also very easy, and it is more flexible than java. in java, you can only use super to call the method that is overwritten. We can not only implement this, but also allow all methods in the subclass to call methods that are overwritten in the parent class. See the following example:

The Code is as follows:


Function parentClass (){
This. method = function (){
Alert ("parentClass method ");
}
}
Function subClass (){
Var method = this. method;
This. method = function (){
Method. call (this );
Alert ("subClass method ");
}
}
SubClass. prototype = new parentClass ();
SubClass. prototype. constructor = subClass;

Var o = new subClass ();
O. method ();


You will find that it is so simple that you define a private variable before defining the override method, and then assign the method defined in the parent class to it, then we can continue to call it later, and this method is private and invisible to the subclass object. This is consistent with the coverage of other advanced languages.

Note that when we call this method in the override method, we need to use the call method to change the execution context to this (although not necessary in this example). If we call this method directly, the execution context becomes a global object.
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.