JavaScript mode (1)--code reuse

Source: Internet
Author: User
Tags function prototype

The development of the program can not be separated from the code reuse, through code reuse to reduce development and maintenance costs, when talking about code reuse, the first thought of inheritance, but inheritance is not the only way to solve code reuse, there are other reusable patterns such as object combination. This section will explain several inheritance patterns for code reuse.

    1. Inheritance Reuse-default mode
    2. Inheritance multiplexing-apply function
    3. Inheritance reuse-Temporary constructs

Default mode for inheritance reuse:

Each JavaScript object is associated with another object, which is the prototype (prototype), and the prototype can be linked to other prototype travel prototype chains, and if no similar method exists for the current object, it is searched along the prototype chain until it is found. The inherited default pattern is implemented through the characteristics of this prototype chain.

The person class defines some properties and methods for the base class

/** * Person base class*/varperson =function () {     This. Name = "";  This. Age = 0;  This. Sex = "";  This. Say =function () {        return"My name is" + This. Name; }}person.prototype.sayhello=function () {    return"Hello";}

Defines an empty sub-function object

/**/varfunction  () {    }

To include the parent class and the parent class prototype in the child object's prototype chain

New Person (); var New  = ' Stephen '; Console.log (Enginneer1.say ());

This type of inheritance implements the disadvantage:

Constructed arguments cannot be passed: it is not feasible to assume that a person base class can accept a constructed pass parameter, whereas an inherited subclass would pass parameters through the parent class construct.

Inheritance multiplexing-apply function

Apply function definition: Used to invoke the current function functionObject and to use the specified object thisObj as a pointer reference inside the function when the function is executed at the same time this . In layman's words, the member of the current function object is copied to the function object referenced by this.

You can copy members of a parent class into a subclass in a subclass construct by using the Apply method, and you can pass parameters through apply instead of binding the prototype chain.

var function () {    person.apply (this);}

Instead of binding the prototype chain, if the method in the parent class prototype cannot be inherited, the following code shows

function () {    return "Hello";}

Using the example

var New  = ' Adam '; Console.log (Enginner2.say ());
Inheritance reuse-Temporary constructs

The above two methods have advantages and disadvantages, but are not perfect, the default inheritance can not pass the construction parameters, and through the Apply () function of the construction of binding and can not inherit the parent prototype chain method, and sometimes the base class has a private method do not want to inherit the quilt class, neither of the above can be done. And the method of temporary constructor can get a perfect solution.

First, a temporary function object is generated, and the prototype chain of the temporary function object is specified as the parent prototype chain by the prototype chain share (note that this is not a binding), and then the prototype chain of the subclass is bound to the prototype chain of the temporary function.

var function  =  new  temp (); var New  = "Stphen"; Console.log (Enginner3.say ());

Through the processing of the above code, the child object has the prototype chain of the temporary function, but does not inherit the parent class defined in the function of the member, so if you want to define a private method can be in the function definition, if you want to define an inheritable method can be in the function prototype, thus the more perfect resolution of inheritance.

Summary:

There are many ways to implement inheritance in JavaScript, and in this section there are three main methods, each of which has its own advantages and disadvantages, which can be agreed upon in the development of a project and, of course, in addition to inheritance, you can use apply to borrow a method to achieve the purpose of reuse, After all, it is not a desirable way to build a long prototype chain in order to use a certain method.

Reference content

"JavaScript mode" Stoyan Stefanov

JavaScript mode (1)--code reuse

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.