Small test on custom JS objects

xiaoxiao2021-03-06  52

I haven't worked for a long time, I have n'thing special, and I'm also cold, Inert episodes. Although I know that JS can customize the object, there is no in-depth study, so those knowledge is someone else, to be my own, then, simple and deep, that is, I do it. Test. The following simple example is mainly creation of the attributes of the object, and B is all general forms of properties, and C is an object attribute. When the object obj is instantiated, the object to C is instantiated, and there are two THIs in Obj (), but the meaning is different. This can be seen from the content from the ALERT below. A object attribute pointing to itself and another pointing. I didn't sure these two THIs were not the same point, and I did a matter of trial. Therefore, when there is an uncertain question, you may wish to verify it. Perhaps the following code will be clearer. Function Obj1 () {this.a = 1 this.b = 2 this.x = 6 this.c = new obj2 ()}

Function Obj2 () {alert (); this.x = 3 this.y = 4}

Objaaa = new Obj1 () Alert (objaaa.x) Alert (objaaa.cx) If you deepen, add a method to Obj1, please pay attention to the difference between the sub-object attribute - NEW keyword method is not : Function obj1 () {this.a = 1 this.b = 2 this.x = 6 this.c = new obj2 () this.funca = function () {Alert ("HahaHAH")}}

Function Obj2 () {alert (); this.x = 3 this.y = 4}

Objaaa = new obj1 () alert (objaaa.x) Objaaaa.funca ()