Javascript Object-Oriented Programming basics polymorphism _ js object-oriented

Source: Internet
Author: User
The implementation method of basic polymorphism in javascript object-oriented programming. You can refer to the following code. Javascript can simulate the encapsulation and inheritance features of the forward object, but unfortunately Javascript has very weak support for Polymorphism features! The polymorphism of other object-oriented languages is generally implemented by method overloading and virtual methods, and Javascript is also implemented through these two methods!

Overload: Javascript is a weak language and supports variable parameters. When we define overload methods, the interpreter cannot distinguish different overload methods by parameter types and number of parameters, therefore, method Overloading is not supported! When a method with the same name is defined successively, the method defined later will overwrite the method defined first!

Since the interpreter cannot distinguish the overload method, you can manually distinguish different methods:

The Code is as follows:


Var MyClass = function (){
Var AddNum = function (a, B ){
Return a + B;
}
Var AddString = function (a, B ){
Return "I am here" + a + B;
}
This. Add = function (a, B ){
If (typeof (a) = "number ")
Return AddNum (a, B );
Else
Return AddString (a, B );
}
}
Var MyObj = new MyClass ();
Var X = MyObj. Add (5, 6 );
Var Y = MyObj. Add ("A", "FFFFFF ");
Alert (X); // result: 11
Alert (Y); // result: I am hereAFFFFFF


Virtual method:

The Code is as follows:


Function BaseClass (){
This. Hello = function (){
Return this. Say ();
}
}
Function MyClassA (){
This. Say = function (){
Return "Hello ";
}
}
Function MyClassB (){
This. Say = function (){
Return "This is MyClassB ";
}
}
MyClassA. prototype = new BaseClass ();
MyClassB. prototype = new BaseClass ();
Var ObjA = new MyClassA ();
Var XX = ObjA. Hello ();
Alert (XX); // result: Hello
Var ObjB = new MyClassB ();
Var YY = ObjB. Hello ();
Alert (YY); // result: This is MyClassB


Due to the execution of Javascript interpretation, you can call the method to be defined in the derived class in the base class. It is equivalent to a virtual method and can achieve simulated polymorphism!
Js overloading and rewriting (overwriting ):
Overload means that "a function with the same name (note that this includes a function) or a method can have multiple implementations, depending on the type and (or) of parameters) the number of parameters to identify ". Overwrite means that "sub-classes can define methods with the same name as the parent class and the same parameter type and number. After these methods are defined, in the instantiated object of the subclass, the methods inherited from the parent class with the same name will be hidden ". Overload is used in English and override is used in English. Now, the concept is introduced here. Have you guessed what I want to say? Hey, Code is cheap. Check the heavy load Code:

The Code is as follows:


// Overload is implemented through the arguments attribute of the Function
Function add (){
Var sum = 0;
For (var I = 0; I <arguments. length; I ++ ){
Sum + = arguments [I];
}
Return sum;
}
Function test (){
Alert (add ());
Alert (add (1, 2 ));
Alert (add (1, 2, 3 ));
}


Through the code running result, the overload of any multiple parameter addition functions is realized. 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. About the characteristics of arguments, I have made a brief introduction, refer to the article: http://blog.csdn.net/zhanglingdll_39/archive/2009/08/20/4465670.aspx.
The following describes how to rewrite javascript:
// Add a static method for the class. inherit indicates that the method inherits from a class.
Function. prototype. inherit = function (baseClass ){
For (var p in baseClass. prototype ){
This. prototype [p] = baseClass. prototype [p];
}
}
// Rewrite js implementation
Function parentClass () {// parent class
}
ParentClass. prototype. method = function (){
Alert ("parentClass method ");
}
Function subClass () {// subClass
}
//

The Code is as follows:


The following sentence is equivalent to subClass. prototype = new parentClass ();
SubClass. inherit (parentClass );
// SubClass. prototype. method = function () {// The subClass overrides the method of the parent class. -- remove the annotation and run it.
// Alert ("subClass method ");
//}
Function test (){
Var obj = new subClass ();
Obj. method ();
}
In this way, the method defined in the subclass overwrites the method inherited from the parent class. You may ask how to call the method of the parent class in the subclass? The implementation is as follows:
// Add a static method for the class. inherit indicates that the method inherits from a class.
Function. prototype. inherit = function (baseClass ){
For (var p in baseClass. prototype ){
This. prototype [p] = baseClass. prototype [p];
}
}
/* Reference: http://menjoy.javaeye.com/blog/127847 */
// Rewrite js implementation
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. inherit (parentClass); // This sentence seems to be equivalent to the previous sentence subClass. prototype = new parentClass (); actually ???? (Comment out the previous line and run this line)
SubClass. prototype. constructor = subClass;
Function test (){
Var obj = new subClass ();
Obj. method ();
}


Now, the introduction to polymorphism is here. Js object-oriented programming is like a vast ocean, vast and boundless. The js object-oriented basics I have written based on other articles can only serve as a reference for beginners to learn. There is no end to learning. I have read several articles from the Internet, and I am very familiar with my technology. For readers who have already gone beyond the learning stage, please refer to the technical articles of high people in the garden. I want to thank the elders in the garden first.

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.