JavaScript Object-oriented new practice prototype inherits _js object-oriented

Source: Internet
Author: User
Tags constructor inheritance instance method
First, you create an instantiated object for the parent class, and then assign the object to the prototype property of the subclass.
In this way, all public instance members in the parent class inherit from the quilt class. And when judged with the instanceof operator, the instantiated object of the subclass belongs to both the subclass and the parent class.

The child class itself is then assigned to its prototype constructor property. (Note: There is no () when the value is assigned!) )
This step is to ensure that when you view the constructor property of the instantiated object of a subclass, you see the definition of the subclass, not the definition of its parent class.

Next, by the result of the call to O.method1 (), we see that in a public instance method that is inherited by a subclass, if a private instance field or a private instance method is invoked, those private instance members that are called are of the parent class.

Similarly, by the result of the call to O.METHOD2 (), we see that the instance method defined in the subclass, if a private instance field or private instance method is invoked, is a subclass of these private instance members that are called.

By the result of the O.method () call, we see that the method defined on the parent prototype will inherit from the quilt class.
The result of the call to O.method3 () shows that the instance method defined in the subclass is not accessible to private instance members defined in the parent class.
Finally, we see that static members are not inherited by the results of the Subclass.staticmethod () call.

2.4 Invoke Inheritance Method
The essence of invocation inheritance is that in the constructor of a subclass, let the constructor method of the parent class execute on the execution context of the subclass, and all content that is manipulated by the This method on the parent constructor methods is actually the content on the instantiated object of the operation's subclass. Therefore, this practice is only to reduce the duplication of code writing.
Copy Code code 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 is a good reflection of how to use the Invoke inheritance method to implement inheritance.
The key to inheriting with calls is only one step:
is to pass the this pointer of the subclass through the call method of the parent class when the subclass is defined. Causes the parent class method to execute in the subclass context.
This way, all public instance members within the parent class that are defined by this method inside the parent class inherit from the quilt class.
When judged with the instanceof operator, the instantiated object of a subclass belongs only to subclasses and not to the parent class.
When you view the constructor property of an instantiated object for a subclass, you see the definition of the subclass, not the definition of its parent class.
Next, the result of the call to the O.METHOD1 () and O.METHOD2 () is the same as the result of the invocation of the prototype inheritance method, and the problem is the same, not repeated here.
By the result of the O.method () call, we see that the method defined on the parent prototype will not inherit from the quilt class.
The result of the call to O.method3 () shows that the instance method defined in the subclass also cannot access the private instance members defined in the parent class.
Finally, we see that static members are also not inherited by the results of the Subclass.staticmethod () call.
Finally, there is one point that is not reflected in this example, that is, by invoking the inheritance method, multiple inheritance can be implemented. That is, a subclass can inherit all public instance members that are defined within the parent class from multiple parent classes.
As a weakly typed language, JavaScript provides rich polymorphism, and JavaScript polymorphism is not comparable to other strongly typed object-oriented languages.
Polymorphic
Overloading and overwriting
Let's first explain the difference between overloading and overwriting. Overloaded English is overload, covering English is override. It is wrong to find that most people on the Internet regard override as an overload. There is a difference between overloading and overwriting.
Overloading means that the functions of the same name (note that there are functions here) or methods can have multiple implementations, and they rely on the number of types and/or parameters of the parameter to distinguish between them.
Overriding means that subclasses can define methods that have the same name as the parent class and have the same parameter types and numbers, and after the definition of these methods, in the instantiated object of the subclass, the methods of the same name that are inherited in the parent class are hidden.
Overload
The parameters of a function in JavaScript are not typed, and the number of arguments is arbitrary, for example, although you can define a:
Copy Code code as follows:

function Add (A, b) {
return a + B;
}

Such a function, but you can still call it to bring in any number of parameters, of course, the parameter type is arbitrary. As for whether or not to go wrong, that's what the function does, and JavaScript doesn't judge which function you're calling based on the number of parameters and the type of parameter you specify.
Therefore, to define an overloaded method, you cannot do so as in a strongly typed language. But you can still implement overloads. Is through the arguments property of the function. For example:
Copy Code code as follows:

function Add () {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
Sum + + arguments[i];
}
return sum;
}

This allows you to implement an overload of 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, to decide what to do later, to implement more complex functions or method overloads. In short, the JavaScript overload is implemented in a function by the user's own manipulation arguments this property.
Covered
It is also easy to implement overrides, for example:
Copy Code code 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 ();

This way, the method defined in the subclass overrides the methods that are inherited from the parent class.
You might say that this kind of overlay is nice, but in Java, the overridden method can call the overridden method (the parent's method), how do you do it here? Also easy, and more flexible in Java than in Java, you can only use super to invoke the secondary overridden method in a method that overrides the overridden method. Not only can we do this, but we can also make it possible for all the methods in the subclass to call the overridden methods in the parent class. Look at the following example:
Copy Code code 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 was so simple, as long as you define a private variable before you define the override method, assign it to the method that will be overridden in the parent class, and then we can continue to call it later, and this is a private method that is not visible to the object of the subclass. This is consistent with the implementation of other high-level languages.

Finally, it is important to note that when we invoke this method in the overwrite method, we need to use the call method to change the execution context to this (although not necessary in this case), if the method is called directly, the execution context becomes the global object.

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.