How to use JavaScript to implement Class Functions

Source: Internet
Author: User

Some time ago, after learning "ajax self-taught manual", I realized that javascript can still be written like this.

 I have learned the concepts of classes in Java, C #, and VB, and classes have functions such as inheritance, encapsulation, and polymorphism. Javascript is not an object-oriented language, but an explanatory language.

But we can also use JavaScript to implement inheritance and polymorphism.

 Javascript implementation class, there are multiple methods.

Method 1: constructor.

Code

  Function  Coder (){
This . Name = ' John ' ;
This . Job = ' ProgramEmployee ' ;
This . Coding = Function ()
{
Alert ( ' I'm writingCode ' );
}
}

VaR Coder = New Coder ();
Alert (CODER. Name );
Coder. coding ();

 

 Method 2: Factory method.

Code

 Function  Createcoderfactory (){
VaR OBJ = New Object ();
OBJ. Name = ' John ' ;
OBJ. Job = ' Programmer ' ;
OBJ. Coding = Function (){
Alert ( ' I am writing code ' );
};
Return OBJ;
}
VaR Coder = Createcoderfactory ();
Alert (CODER. Name );
Coder. coding ();

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.

Code

  Function  Coder (){}
Coder. Prototype. Name = ' John ' ;
Coder. Prototype. Job = ' Programmer ' ;
Coder. Prototype. Coding = Function (){
Alert ( ' I am writing code ' );
};
VaR Coder = New Coder ();
Alert (CODER. Name );
Coder. coding ();

Note: The book says: the disadvantage of prototype chain is that all its attributes are shared, and all other attributes of a prototype chain will change as long as one instance changes.The test is as follows:

  VaR  Coder1  =    New  Coder ();
VaR Coder2 = New Coder ();
Alert (coder1.name ); /* Show "Wang" */
Coder2.name = ' Old Wang ' ;
Alert (coder1.name ); /* This shows "Xiao Wang". If it says in the book, it should show "Lao Wang "*/
Alert (coder2.name ); /* This shows "Old Wang" */

Alert (coder1.name); if "Old Wang" is displayed in the book, but "Little Wang" is displayed here, an error occurs in the book.

  

Method 4: hybrid mode.

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

  Function  Coder (){
This . Name = ' John ' ;
This . Job = ' Programmer ' ;
}
Coder. Prototype. Coding = Function (){
Alert ( ' I am writing code ' );
};

 

Method 5: Dynamic Source chain.

There is another way to solve the first three shortcomings.

Code

  Function  Coder (){
This . Name = ' John ' ;
This . Job = ' Programmer ' ;
If ( Typeof (CODER. _ init) = ' Undefined ' ){
This . Coding = Function ()
{
Alert ( ' I am writing code ' );
};
This . _ Init = True ;
}
}

what about this method, when used for the first time, because _ init is not initialized, the following code is executed to instantiate the coding function. It will not be executed in the future, so that the function will be instantiated only once.

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.