Method 1 for writing classes in Javascript (native)
Source: Internet
Author: User
There are many methods to implement the javascript writing class on the Internet. The following are a summary. I learned and summarized it myself. 1. construction method 2. prototype 3. the constructor + prototype hybrid method is used to analyze the advantages and disadvantages of the above methods: & amp; bull; constructor method is the most basic and... syntaxHighlighter. there are many methods for implementing the javascript writing class on the all () network. The summary is similar to the following. I learned and summarized it myself.
1. constructor Method
2. Prototype
3. constructor + prototype hybrid mode
Now let's take a look at the advantages and disadvantages of the above methods:
• Constructor Method
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.
• Prototype
1 // create a Student class
2 // attributes and methods are set 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.
• Hybrid mode
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 );
3}
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.
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.