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
-
- // Constructor defines classes. Advantages: Multiple instance objects do not share class attribute values. Disadvantages: each instance object generates a function say
-
- Function user (){
- This. Username ="Zhangsan";// This. cannot be lost
-
-
- This. Say = function (){// This. cannot be lost
-
- Alert ("Username :"+This. Username );// This. cannot be lost
-
- }
-
- // The method described below is incorrect.
-
- // Function say (){
- // Alert ("username:" + this. username );
-
- //}
-
- }
-
-
- VaR user =NewUser ();
-
- User. Username ="Lisi";
-
- User. Say ();// Username: Lisi
-
-
- VaR user1 =NewUser ();
-
- 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
-
- // The attribute values of multiple instance objects that do not share classes are as follows:
-
- Function user (){
-
- This. Username =NewArray ();// This. cannot be lost
-
- This. Say = function (){// This. cannot be lost
-
- Alert ("Username :"+This. Username );// This. cannot be lost
-
- }
-
- }
-
-
- VaR user =NewUser ();
-
- User. username. Push ("Zhangsan");
-
- User. Say ();// Username: zhangsan
-
- VaR user1 =NewUser ();
-
- 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
-
- // 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.
-
- Function user (){
-
- }
- User. Prototype. Username ="Zhangsan";
-
- User. Prototype. Say = function (){
-
- Alert ("Username :"+This. Username );
-
- }
-
-
- VaR user =NewUser ();
-
- User. Username ="Lisi";
-
- User. Say ();// Username: Lisi
-
-
- VaR user1 =NewUser ();
- 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
-
- // 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:
-
- Function user (){
-
- }
-
- User. Prototype. Username =NewArray ();
-
- User. Prototype. Say = function (){
- Alert ("Username :"+This. Username );
-
- }
-
-
- VaR user =NewUser ();
-
- User. username. Push ("Zhangsan");
-
- User. Say ();// Username: zhangsan
-
-
- VaR user1 =NewUser ();
-
- 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
- // 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.
- function user () {
- This . username = "zhangsan" ;
- }
- User. Prototype. Say = function () {
- alert ( "username: " + This . username);
- }
- var user = New User ();
- 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
-
- /// 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.
-
-
- Function user (){
-
- This. Username ="Zhangsan";
-
- If(Typeof user. Flag ="Undefined"){
-
- Alert ("Execute ...");
-
- User. Prototype. Say = function (){
-
- Alert ("Username :"+This. Username );
-
- }
-
-
- User. Flag =True;
- }
-
- }
-
-
- VaR user1 =NewUser ();// Execute...
-
- VaR user2 =NewUser ();// If execute... is not printed, the method is created only once, that is, the method will generate only one
-
- User1.say ();// Username