Implementation of JavaScript writing classes

Source: Internet
Author: User

There are many methods to implement javascript writing on the Internet. I have summarized the following methods.

Constructor method; prototype; constructor method + prototype hybrid mode

Now let's take a look at the advantages and disadvantages of the above methods:

ConstructorMethod

This is the most basic and most similar way to writing a class in Java. The Code is as follows:

 
 
  1. // Create a Student class
  2. Function Student (name ){
  3. This. name = name;
  4. This. sayName = function (){
  5. Alert (this. name );
  6. };
  7. }
  8. // New two different Student.
  9. Var jimmy = new Student ('Jimmy ');
  10. Var henry = new Student ('henry ');
  11. Jimmy. sayName (); // display jimmy
  12. Henry. sayName (); // display henry

This method is simple and clear, and it also fits the appetite of Java er. However, every new object will allocate a sayName method in the memory, and the performance is not very good.

PrototypeMethod

 
 
  1. // Create a Student class
  2. // Set attributes and methods through Student. prototype
  3. Function Student (name ){
  4. Student. prototype = name;
  5. Student. prototype. sayName = function (){
  6. Alert (this. name );
  7. }
  8. }
  9. // New two different Student.
  10. Var jimmy = new Student ('Jimmy ');
  11. Var henry = new Student ('henry ');
  12. Jimmy. sayName (); // display henry !!!
  13. Henry. sayName (); // display henry !!!

Maybe the code executed is different from the expectation of some kids shoes. henry is displayed in both alert! In fact, it is easy to understand. both attributes and methods are set through prototype. the same attribute or method of different objects points to the same memory, so henry is set after jimmy. so henry overwrites jimmy.

HybridMethod

The constructor can allocate different memory for each object of the same class, which is suitable for setting attributes when writing classes, however, when setting the method, we need to share the same memory with different objects in the same class. it is best to use a prototype for writing methods. therefore, when writing a class, you need to mix the constructor and prototype. look at the Code:

 
 
  1. // Create a Student class
  2. // Set attributes through Constructor
  3. // Method set through Student. prototype
  4. Function Student (name ){
  5. This. name = name;
  6. Student. prototype. sayName = function (){
  7. Alert (this. name );
  8. }
  9. }
  10. // New two different Student.
  11. Var jimmy = new Student ('Jimmy ');
  12. Var henry = new Student ('henry ');
  13. Jimmy. sayName (); // display jimmy
  14. Henry. sayName (); // display henry

So far so good. Different objects of the same class have their own memory and the methods share the same memory. In fact, there is still a small problem:

Every new object is executed once.

 
 
  1. Student.prototype.sayName = function(){  
  2.      alert(this.name);  

This causes unnecessary repeated operations. You can set a flag bit in the class. During the first execution, set true for this flag bit. If it is true, the prototype method is no longer set.

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.