Javascript-Class Construction)

Source: Internet
Author: User
Tags prototype definition
Javascript --- Class Construction

Keywords:Javascript--- Class Construction

Javascript There are four main ways to build a class in
1. constructor definition class
2. Prototype definition class
3. Create a class by combining constructor and prototype
4. Dynamic Prototype

Each has its own advantages and disadvantages, as shown below:
1.Constructor defines classes. Advantages: Multiple instance objects do not share the attribute values of classes. Disadvantages: each instance object generates a function say

Java Code
  1. // Constructor defines classes. Advantages: Multiple instance objects do not share class attribute values. Disadvantages: each instance object generates a function say
  2. Function user (){
  3. This. Username ="Zhangsan";// This. cannot be lost
  4. This. Say = function (){// This. cannot be lost
  5. Alert ("Username :"+This. Username );// This. cannot be lost
  6. }
  7. // The method described below is incorrect.
  8. // Function say (){
  9. // Alert ("username:" + this. username );
  10. //}
  11. }
  12. VaR user =NewUser ();
  13. User. Username ="Lisi";
  14. User. Say ();// Username: Lisi
  15. VaR user1 =NewUser ();
  16. User1.say ();// Username: zhangsan, which is not affected by the user object
// Constructor defines the class. Advantage: Multiple instance objects do not share the class property value. disadvantage: each instance object generates a function sayfunction user () {This. username = "zhangsan"; // This. this cannot be lost. say = function () {// This. alert ("username:" + this. username); // This. cannot be lost} // The following annotation is incorrect // function say () {// alert ("username:" + this. username); //} var user = new user (); User. username = "Lisi"; user. say (); // username: lisivar user1 = new user (); user1.say (); // username: zhangsan, not affected by the user object

// Attributes of classes not shared among multiple instance objects: Java code

  1. // The attribute values of multiple instance objects that do not share classes are as follows:
  2. Function user (){
  3. This. Username =NewArray ();// This. cannot be lost
  4. This. Say = function (){// This. cannot be lost
  5. Alert ("Username :"+This. Username );// This. cannot be lost
  6. }
  7. }
  8. VaR user =NewUser ();
  9. User. username. Push ("Zhangsan");
  10. User. Say ();// Username: zhangsan
  11. VaR user1 =NewUser ();
  12. User1.say ();// The username of user1 is null and is not zhangsan, because the attribute value of user1 is not affected by the user.
 
// The attribute values of multiple instance objects that do not share the class are as follows: function user () {This. username = new array (); // This. this cannot be lost. say = function () {// This. alert ("username:" + this. username); // This. cannot be lost} var user = new user (); User. username. push ("zhangsan"); User. say (); // username: zhangsanvar user1 = new user (); user1.say (); // username of user1 is blank, not zhangsan, because the attribute value of user1 is not affected by the user

2.The prototype defines the class. disadvantage: if the class property value is of the reference type (non-number or string type), multiple instance objects are shared.Java code

  1. // Define the class in the prototype mode. disadvantage: if the class property value is of the reference type (non-number or string type), multiple instance objects are shared.
  2. Function user (){
  3. }
  4. User. Prototype. Username ="Zhangsan";
  5. User. Prototype. Say = function (){
  6. Alert ("Username :"+This. Username );
  7. }
  8. VaR user =NewUser ();
  9. User. Username ="Lisi";
  10. User. Say ();// Username: Lisi
  11. VaR user1 =NewUser ();
  12. User1.say ();// Username: zhangsan
 
// Define the class in the prototype mode. disadvantage: if the class property value is of the reference type (non-number or string type), multiple instance objects share function user () {} user. prototype. username = "zhangsan"; user. prototype. say = function () {alert ("username:" + this. username);} var user = new user (); User. username = "Lisi"; user. say (); // username: lisivar user1 = new user (); user1.say (); // username: zhangsan

If the attribute values of a class are of the reference type (non-number or string type), multiple instance objects are shared: Java code

  1. // If the attribute value of the class is of the reference type (not of the number or string type), multiple instance objects are shared, as shown below:
  2. Function user (){
  3. }
  4. User. Prototype. Username =NewArray ();
  5. User. Prototype. Say = function (){
  6. Alert ("Username :"+This. Username );
  7. }
  8. VaR user =NewUser ();
  9. User. username. Push ("Zhangsan");
  10. User. Say ();// Username: zhangsan
  11. VaR user1 =NewUser ();
  12. User1.say ();// Username: zhangsan. Because the user1 attribute is also affected by the user, user1 and user point to the same reference, that is, share the same attribute.
// If the attribute value of the class is of the reference type (not of the number or string type), multiple instance objects are shared, as shown in function user () {} user. prototype. username = new array (); User. prototype. say = function () {alert ("username:" + this. username);} var user = new user (); User. username. push ("zhangsan"); User. say (); // username: zhangsanvar user1 = new user (); user1.say (); // username: zhangsan, because the user1 attribute is also affected by the user, user1 and user point to the same reference, that is, share the same attribute

3.Create a class by combining the constructor and the prototype: the method of the class only produces one, does not produce a large number of methods, and the attributes are not shared. The disadvantage is that the separation of attributes and methods is not very good.

Java code
    1. // creates a class by combining constructor and prototype. Advantages: only one class method is generated, and no large number of methods are generated, at the same time, attributes are not shared. Disadvantages: it is not very good to separate attributes from methods.
    2. function user () {
    3. This . username = "zhangsan" ;
    4. }
    5. User. Prototype. Say = function () {
    6. alert ( "username: " + This . username);
    7. }
    8. var user = New User ();
    9. alert (user. username);
// Create a class by combining constructor and prototype: advantage: only one class method is generated, no large number of methods are generated, and attributes are not shared; disadvantage: it is not very good to separate attributes from methods. Function user () {This. username = "zhangsan";} user. prototype. say = function () {alert ("username:" + this. username);} var user = new user (); alert (user. username );

4.Dynamic Prototype: Advantages: only one class method is generated, and a large number of methods are not generated. attributes are not shared, and attributes and methods are not defined separately.

Java code
  1. /// Dynamic prototype: advantage: only one class method is generated, and a large number of methods are not generated. attributes are not shared, and attributes and methods are not defined separately.
  2. Function user (){
  3. This. Username ="Zhangsan";
  4. If(Typeof user. Flag ="Undefined"){
  5. Alert ("Execute ...");
  6. User. Prototype. Say = function (){
  7. Alert ("Username :"+This. Username );
  8. }
  9. User. Flag =True;
  10. }
  11. }
  12. VaR user1 =NewUser ();// Execute...
  13. VaR user2 =NewUser ();// If execute... is not printed, the method is created only once, that is, the method will generate only one
  14. User1.say ();// Username
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.