Implementation _javascript techniques for a JavaScript inheritance

Source: Internet
Author: User
Author: Yin Wei-ming

blog:http://my.donews.com/yinwm/

As my previous article says, for JavaScript, a class is a function, and his class method (that is, static) is part of the function, and the instance method is above the prototype.
function ClassA () {
}

Classa.staticmethod = function () {
}

ClassA.prototype.instanceMethod = function () {
}

In my implementation, the inheritance of a class is a copy of all the class methods of the parent class, and the class has a static method of the parent class.
Then let the prototype.prototype of the subclass point to the prototype of the parent class.
You can then rewrite some of the methods according to your needs.
function ClassB () {
}
Classb.staticmethod = Classa.staticmethod;
ClassB.prototype.prototype = Classa.prototype;
ClassB.prototype.instanceMethod = function () {
Method 2
}

For subclasses, a prototype chain is used to implement the inheritance of instance methods of the method. This implementation is chosen because subclasses are overriding some of these methods. And prototype is a reference, so the direct writing classb.prototype = Classa.prototype, will be in the rewrite ClassB instance method, destroys the ClassA instance method. The modified method masks the parent class.

The order of search methods is, InstanceA.prototype.method-> classa.prototype.
In this case, the inheritance of the class method has already been implemented, and you now need to implement the method that invokes the parent class in the subclass.
For Java, this is a common use.
public void Method () {
Super.method ();
}
In Javsscript, in order to implement such functions, one parent's reference must be retained, pointing to Parentclass.prototype.
ClassB.prototype.parent = Classa.prototype.
Then call This.parent.method.call (this) in the INSTANCEB, and you can use the method of the parent class. Called using call to upload your data to the parent class. More beautiful solution, which I had not thought of.

So the complete code is
function ClassA () {
}

ClassA.prototype.method1 = function () {
}

Classa.staticmethod = function () {
}

function ClassB () {
}

Classb.staticmethod = Classa.staticmethod;

ClassB.prototype.prototype = ClassB.prototype.parent = Classa.prototype;

This I abstracted out a extend method,

var lcore = function () {
}

Lcore.extend = function (destination, source) {
Copy all Functons
For (Var prop in source) {
if (prop = = "Prototype") {
Continue
}

Destination.prototype[prop] = Source[prop];
}

Make a reference for parent and reference prototype
Destination.prototype.prototype = Destination.prototype.parent = Source.prototype;

return destination;
}

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.