Simple talk about JavaScript code reuse pattern _javascript Skill

Source: Internet
Author: User

Code reuse has a well-known principle that is proposed by GOF: Prioritize the use of object combinations rather than class inheritance. In JavaScript, there is no concept of a class, so the reuse of code is not limited to class inheritance. There are many ways to create objects in JavaScript, there are constructors, you can use new to create objects, and you can modify objects dynamically. The non-class inheritance of JavaScript (which can be called Modern inheritance mode) is also a lot of reuse methods, such as using other objects to combine the required objects, objects mixing technology, borrowing and reusing the methods needed.

Class-Type Inheritance mode-default mode

Two constructors examples of parent and child:

Copy Code code as follows:

function Parent (name) {
THIS.name = name| | " Adam ";
}
Parent.prototype.say = {
return this.name;
};
function child (name) {
}
Inherit (child,parent);

The following is an implementation of the reusable inheritance function inherit ():

Copy Code code as follows:

function inherit (c,p) {
C.prototype = new P ();
}

Here the prototype attribute should point to an object, not a function, so he must point to an instance created by the parent constructor rather than to the constructor itself.

After that, when you create a child object, you get his functionality from the parent instance by using a prototype:

Copy Code code as follows:

var kid =new Child ();
Kid.say ()//"Adam"

To invoke the prototype chain after inheritance:

To further add kid properties:

Copy Code code as follows:

var kid = new Child ();
Kid.name = "Patrick";
Kid.say ()//"Patrick"

The change of the prototype chain:

You can find name in the properties of your object, and you don't have to look up the prototype chain.

One disadvantage of using the above pattern is that it inherits the properties of two objects at the same time, the properties that are added to this and the prototype properties. Most of the time, you don't need these properties of yourself.

Another disadvantage is that using inherit () inheritance does not support passing parameters to a child constructor, for example:

Copy Code code as follows:

var s = new Child ("Seth");
S.say ()//"Adam"

This result is not expected, although the child constructor can pass parameters to the parent constructor, but this inheritance mechanism must be rerun every time a child object is needed, and inefficient because the parent object will eventually be recreated.

This article is here first, and we will continue to update the remaining patterns of the JavaScript code reuse pattern.

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.