JavaScript code reuse mode

Source: Internet
Author: User

Code reuse has a well-known principle, which is GOF: Prioritize the use of object combinations rather than class inheritance. In JavaScript, there is no concept of class, so the reuse of code is not limited to class-type 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. JavaScript's non-class inheritance (which can be referred to as modern inheritance mode) is also a lot of multiplexing methods, such as using other objects to assemble the required objects, mixing the objects into technology, and borrowing and reusing the required methods.

Class inheritance Mode-default mode

Two constructors examples of parent and child:

function Parent (name) {    this. Name = name| | " Adam "= {    returnthis. name;}; function Child (name) {}inherit (child,parent);

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

function inherit (c,p) {    new  P ();}

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

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

var kid =New child (); Kid.say (); // "Adam"

To invoke a prototype chain after inheritance:

To add a kid's properties further:

var New  = "Patrick"; Kid.say (); // "Patrick"

Change of prototype chain:

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

One drawback of using the above pattern is that it inherits the properties of two objects, the properties that are added to this and the prototype properties. Most of the time, you don't need these attributes of your own.

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

var New Child ("Seth"); S.say (); // "Adam"

This result is not expected, although the child constructor can pass parameters to the parent constructor, but it must be re-executed each time a child object is required, and is inefficient because the parent object is eventually recreated.
Cond

JavaScript code reuse mode

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.