1. Private attribute Analysis
1 function{3 //Private attribute 4 var privatename = "Private"; 5 6 this.setprivatename = function c9/>{8 privatename = }10 this.getprivatename = function {return}16 $var object1 = new My Class (), var object2 = new MyClass (), Object1.setprivatename ("Object1"),object1.setprivatename (" Object2 "alert (Object1.getprivatename ()); 23//pop-up object224 alert (Object2.getprivatename ());
The above code proves that private properties cannot be shared by different objects and can be used normally
2. Window properties, method analysis
1 function{3 //Window property 4 windowname = "Window"; 5 6 this.setwindowname = function {8 windowname = }10 this.getwindowname = function {return}16 var object1 = new MyC Lass (), var object2 = new MyClass (), Object1.setwindowname ("Object1"), Object1.setwindowname ("Object2 "Alert (Object1.getwindowname ()); 23//Eject object224 alert (Object2.getwindowname ());
The above code proves that the window property is shared by different objects
There are more surprises.
1 functionMyClass1 (2){3//Window property 4 windowname = "Window"; 5 6 this.setwindowname = function(value) 7{8 Windowname =Value 9}10 This.getwindowname = function() 12{returnWindowname;14}15 Windowmethod = function() 17{Alert ("MyClass1")); 19}20 This.visitwindowmethod = function() 22{23Windowmethod (); 24}25}26 functionMyClass2 (28) {//Window property: Windowname = "window" ; this.setwindowname = function (value) over {Windowna me = value;35 }36 this.getwindowname = function () {$ return windowname;40 }41 Windowmethod = function () {MyClass2 ); }46 This.visitwindowmethod = functio n () net {Windowmethod (); }51 }52-var object1 = new MyClass1 (); var object2 = new MyClass2 (), Object1.setwindowname ("Object1" ), Object1.setwindowname ("Object2" ); 57// Popup object258 alert (Object1.getwindowname ()); 59//Eject object260 alert (Object2.getwindowname ()); 61// Eject MyClass262 Object1.visitwindowmethod (); 63//Eject MyClass264 Object2.visitwindowmethod ();
Well, Windowname and Windowmethod, although defined within the class, actually equal the windowname and Windowmethod defined for the window, and all access is to properties and methods of the Window object. So when another object or class defines the same windowname and Windowmethod, the previous definition is overwritten.
There is no difference between the properties of Windowname and window, andWindowmethod is somewhat different from the method of window. Windowmethod can access private properties and private methods within the class, which is not possible with window methods. This is a good feature, but this feature is not very useful.
Let's do another experiment.
1 function MyClass1 () 2 {3//private property 4 var privatename = "Myclass1private" ; 5 6 Windowmethod = function () 7 {8 alert (privatename); 9 }10 This.visitwindowme Thod = function () {windowmethod (); }15 }16-function MyClass2 () {1 9//Private attribute var privatename = "Myclass2private" ; windowmethod = function () + { Aler T (privatename); + }26 this.visitwindowmethod = function () {}31 windowmethod (); }32 var object1 = new MyClass1 (); var object2 = new MyClass2 (); 35//Eject Myclass2private36 obje Ct1.visitwindowmethod (); 37//Eject Myclass2private38 Object2.visitwindowmethod ();
Are you surprised that the visitors are all the properties of the MyClass2.
Windowname and Windowmethod to use caution, this method of non-VAR definition will be a very big pit, so we recommend not to use.
3, this definition of the public property analysis
1 function{3 //Public 4 this.publicname = ' public '; } 6 7 var object1 = new MyClass (); 8 var object2 = new MyClass (); 9 object1.publicname = "Object1"
;10 object2.publicname = "Object2"
alert (object1.publicname);//object214 alert (object2.publicname);
There is nothing to say about this, as in other languages.
4, prototype definition of public property analysis
1 functionMyClass ()2 {3 4 }5MyClass.prototype.publicName = "Public";6 varObject1 =NewMyClass ();7 varObject2 =NewMyClass ();8 9 // PublicTen alert (object1.publicname); One // Public A alert (object2.publicname); -Object1.publicname = "Object1"; -Object2.publicname = "Object2"; the //Object1 - alert (object1.publicname); - //Object2 - alert (object2.publicname); + // Public - alert (MyClass.prototype.publicName); + AObject1.constructor.prototype.publicName = "Object1prototype"; atObject2.constructor.prototype.publicName = "Object2prototype"; - //Object1 - alert (object1.publicname); - //Object2prototype - alert (object1.constructor.prototype.publicName); - //Object2 in alert (object2.publicname); - //Object2prototype to alert (object2.constructor.prototype.publicName); + //Object2prototype -alert (MyClass.prototype.publicName);
This example believes that there have been some surprises, which are related to the way JavaScript is generated, and we can think of JavaScript as a template, each time the object is generated, the properties , methods, and classes defined within the class are defined by prototype, Methods (private properties, private methods, public properties, public methods) are copied to the object without copying the static and static methods defined outside the class.
the only association between objects and templates after the copy is complete is constructor, the object can access the template through constructor.
The above example should be well understood, constructor.prototype.publicName is changing the properties in the template, Object1.publicname just changes the properties in the Object1 object's copy.
5. Static attribute Analysis
It also explains the principle of static property, which is actually a property of the MyClass template, and the object does not contain this attribute except in the template. objects that want to access static properties can only be passed constructor.
1 functionMyClass ()2 {3 4 }5Myclass.staticname = "Static";6 varObject1 =NewMyClass ();7 varObject2 =NewMyClass ();8Object1.staticname = "Object1";9Object2.staticname = "Object2";Ten //Object1 One alert (object1.staticname); A //Object2 - alert (object2.staticname); - //Static the alert (myclass.staticname) - -Object1.constructor.staticName = "Object1"; -Object2.constructor.staticName = "Object2"; + //Object2 - alert (object1.constructor.staticName); + //Object2 A alert (object2.constructor.staticName); at //Object2 -alert (myclass.staticname);
This example is a good illustration of how static properties are used, objects are accessed through constructor, and classes can be accessed directly. The above object1.staticname = "Object1" actually adds a public property named Staticname to Object1 and does not assign a value to the static property.
JavaScript Object-oriented (2)--attribute analysis and validation