Javascript implementation class, there are multiple methods.

Source: Internet
Author: User

Method 1: constructor.

  1. Function coder (){
  2. This. Name = 'wang ';
  3. This. Job = 'programmer ';
  4. This. coding = function ()
  5. {
  6. Alert ('Code I am writing ');
  7. }
  8. }
  9. VaR coder = new coder ();
  10. Alert (CODER. Name );
  11. Coder. coding ();

Copy code

Method 2: Factory method.

  1. Function createcoderfactory (){
  2. VaR OBJ = new object ();
  3. OBJ. Name = 'wang ';
  4. OBJ. Job = 'programmer ';
  5. OBJ. coding = function (){
  6. Alert ('Code I am writing ');
  7. };
  8. Return OBJ;
  9. }
  10. VaR coder = createcoderfactory ();
  11. Alert (CODER. Name );
  12. Coder. coding ();

Copy code

However, 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.

Method 3: prototype chain.

  1. Function coder (){}
  2. Coder. Prototype. Name = 'John ';
  3. Coder. Prototype. Job = 'programmer ';
  4. Coder. Prototype. coding = function (){
  5. Alert ('Code I am writing ');
  6. };
  7. VaR coder = new coder ();
  8. Alert (CODER. Name );
  9. Coder. coding ();

Copy code

However, the disadvantage of the original row 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:

  1. VaR coder1 = new coder ();
  2. VaR coder2 = new coder ();
  3. Alert (coder1.name);/* display "John "*/
  4. Coder2.name = 'old Wang ';
  5. Alert (coder1.name);/* shows "Old King "*/
  6. Alert (coder2.name);/* This also displays "Old King "*/

Copy code

Method 4: hybrid mode.

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

  1. Function coder (){
  2. This. Name = 'wang ';
  3. This. Job = 'programmer ';
  4. }
  5. Coder. Prototype. coding = function (){
  6. Alert ('Code I am writing ');
  7. };

Copy code

Method 5: Dynamic Source chain.

There is another way to solve the first three shortcomings.

  1. Function coder (){
  2. This. Name = 'wang ';
  3. This. Job = 'programmer ';
  4. If (typeof (CODER. _ init) = 'undefined '){
  5. This. coding = function ()
  6. {
  7. Alert ('Code I am writing ');
  8. };
  9. This. _ init = true;
  10. }
  11. }

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.