JavaScript Object-oriented programming based polymorphism _JS object-oriented

Source: Internet
Author: User
Tags constructor
JavaScript has been able to simulate object-oriented encapsulation and inheritance, but unfortunately JavaScript's support for polymorphic features is very weak! Other object-oriented language polymorphism is generally implemented by method overload and virtual method to achieve polymorphism, JavaScript is also through these two ways to achieve!

Overloading: Because JavaScript is a weakly typed language and supports variable parameters, when we define overloaded methods, the interpreter cannot differentiate between overloaded methods by parameter types and number of parameters, so the method overload is unsupported! When a method of the same name is defined successively, the defined method overrides the first defined method!

Since the interpreter cannot distinguish between overloaded methods, you can manually distinguish between the different methods:
Copy Code code 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); Results: 11
alert (Y); Result: I am HEREAFFFFFF

Virtual method:
Copy Code code 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

Because JavaScript interprets the characteristics of execution, you can then call the method that will be defined in the derived class, and the base class method equivalent to virtual methods, you can achieve analog polymorphism!
JS overloads and Overrides (overwrite):
Overloading means that "a function of the same name (note that this includes a function) or a method can have multiple implementations, which depend on the number of types and/or parameters of the parameter to distinguish between them." overriding (overriding) means that a subclass can define a method that has the same name as the parent class, and that the parameter types and numbers are the same, and that 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. Overloaded English is overload, covering English is override. Well, introduction to the concept here, you guessed what I'm going to say? Hey, the code is cheap. See Overloaded code:
Copy Code code as follows:

Implementing overloads by using the arguments property of a 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 to run the results, so that any number of parameters of the addition function of the overload. 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. On the characteristics of arguments, I have done a brief introduction, reference to the humble text: http://blog.csdn.net/zhanglingdll_39/archive/2009/08/20/4465670.aspx.
The following key to understand the implementation of JS rewrite:
To add a static method to a class inherit represents inheriting from a class
Function.prototype.inherit = function (BaseClass) {
For (var p in Baseclass.prototype) {
this. Prototype[p] = baseclass.prototype[p];
}
}
JS Implementation rewrite
function ParentClass () {//Parent class
}
ParentClass.prototype.method = function () {
Alert ("ParentClass method");
}
function Subclass () {//Subclass
}
//
Copy Code code as follows:

The following sentence and Subclass.prototype = new ParentClass ();
Subclass.inherit (ParentClass);
SubClass.prototype.method = function () {//Subclass overrides method of parent class--get rid of comment run try
Alert ("Subclass Method");
// }
function Test () {
var obj = new subclass ();
Obj.method ();
}
This way, the method defined in the subclass overrides the methods that are inherited from the parent class. This is what you might ask, how do you call the parent class method in subclasses? OK, look at the implementation as follows:
To add a static method to a class inherit represents inheriting from a class
Function.prototype.inherit = function (BaseClass) {
For (var p in Baseclass.prototype) {
this. Prototype[p] = baseclass.prototype[p];
}
}
/* Reference article: http://menjoy.javaeye.com/blog/127847 * *
JS Implementation rewrite
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 and the previous sentence Subclass.prototype = new ParentClass (), equivalence, in fact???? (Comment on line, run this line to see)
SubClass.prototype.constructor = subclass;
function Test () {
var obj = new subclass ();
Obj.method ();
}

Well, here's the introduction to polymorphism. JS object-oriented programming like the vast expanse of the vast ocean, I have three references to other people's articles to write the JS Object-oriented Foundation can only be used as a reference for beginners to learn. Learning, referring to the online several bosses of the cattle, know their own technical shallow, for has gone beyond the understanding stage of the reader, or look at the garden of High man technical articles bar. I'm going to bese in the garden first.
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.