Multiple methods of JavaScript implementation class _ javascript tips-js tutorial

Source: Internet
Author: User
Examples of multiple methods of JavaScript implementation classes. For more information, see constructor.

The Code is as follows:


Function coder ()
{
This. name = 'modern magic ';
This. job = 'web developer ';
This. coding = function ()
{Alert ('Code I am writing ');}
}

Var coder = new coder ();
Alert (coder. name );
Coder. coding ();


Factory method

The Code is as follows:


Function createCoderFactory ()
{
Var obj = new Object ();
Obj. name = 'modern magic ';
Obj. job = 'programmer ';
Obj. coding = function ()
{
Alert ('Code I am writing ');
};
Return obj;
}
Var coder = createCoderFactory ();
Alert (coder. name );
Coder. coding ();

Both the factory method and constructor method share the same disadvantage, that is, every function of this class is instantiated every time an instance is created.

Prototype chain

The Code is as follows:


Function coder (){}
Coder. prototype. name = 'modern magine ';
Coder. prototype. job = 'programmer ';
Coder. prototype. coding = function (){
Alert ('Code I am writing ');
};
Var coder = new coder ();
Alert (coder. name );
Coder. coding ();


The disadvantage of a prototype chain is that all its attributes are shared, and all other attributes of an instance will be changed as long as one instance changes. For example:

The Code is as follows:


Var coder1 = new coder ();
Var coder2 = new coder ();
Alert (coder1.name);/* display modern magic */
Coder2.name = 'nowamagic ';
Alert (coder1.name);/* display nowamagic */
Alert (coder2.name);/* This also displays nowamagic */


Hybrid mode
The above three methods all have their own shortcomings, so we need to improve them.

The Code is as follows:


Function coder ()
{
This. name = 'modern magic ';
This. job = 'programmer ';
}
Coder. prototype. coding = function (){
Alert ('Code I am writing ');
};


Dynamic original chain
There is another way to solve the first three shortcomings.

The Code is as follows:


Function coder ()
{
This. name = 'modern magic ';
This. job = 'programmer ';
If (typeof (coder. _ init) = 'undefined ')
{
This. coding = function ()
{
Alert ('Code I am writing ');
};
This. _ init = true;
}
}

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.