1. You can dynamically add object attributes and delete object attributes.
<SCRIPT type = "text/JavaScript">
VaRObject =NewObject ();
Object. Username = "zhangsan ";
Alert (object. username );
DeleteObject. Username;
Alert (object. username );
</SCRIPT>
II. The most common way to define objects
<SCRIPT type = "text/JavaScript">
VaRObject = {Username: "zhangsan", password: 123 };
Alert (object. username );
Alert (object. Password );
</SCRIPT>
Iii. Factory Mode
<SCRIPT type = "text/JavaScript">
FunctionCreateobject (){
VaRObject =NewObject ();
Object. Username = "zhangsan ";
Object. Password = "123 ";
Object. Get =Function(){
Alert (This. Username + "," +This. Password );
}
}
</SCRIPT>
VaRObj1 = Createobject ();
VaRObj2 = Createobject ();
Obj1.get ();
Constructor with parameters:
FunctionCreateobject (username, password ){
VaRObject =NewObject ();
Object. Username = username;
Object. Password = password;
Object. Get =Function(){
Alert (This. Username + "," +This. Password );
}
ReturnObject;
}
VaRObject1 = Createobject ("zhangsan", "123 ");
Object1.get ()
Share a function object with multiple objects, rather than having one function object for each object:
FunctionGet (){
Alert (This. Username + "," +This. Password );
}
FunctionCreateobject (username, password ){
VaRObject =NewObject ();
Object. Username = username;
Object. Password = password;
Object. Get = get;
ReturnObject;
}
VaRObject1 = Createobject ("zhangsan", "123 ");
VaRObject2 = Createobject ("Lisi", "456 ");
Object1.get ();
Object2.get ();
Iv. constructor mode:
FunctionPerson (){
This. Username = "zhangsan ";
This. Password = "123 ";
This. Getinfo =Function(){
Alert (This. Username + "," +This. Password );
}
}
VaRP1 =NewPerson ();
P1.getinfo ();
Constructor with parameters:
FunctionPerson (username, password ){
This. Username = username;
This. Password = password;
This. Getinfo =Function(){
Alert (This. Username + "," +This. Password );
}
}
VaRP1 =NewPerson ("zhangsan", 2 );
P1.getinfo ();
V. Prototype)
FunctionPerson (){
}
Person. Prototype. Username =NewArray ();
Person. Prototype. Password = "123 ";
Person. Prototype. getinfo =Function(){
Alert (This. Username + "," +This. Password );
}
VaRP1 =NewPerson ();
VaRP2 =NewPerson ();
P1.username. Push ("zhangsan ");
P1.username. Push ("Lisi ");
P1.password = "456 ";
P1.getinfo ();
P2.getinfo ();
You cannot assign an initial value to the attribute simply by using the prototype. You can only change the value after the object is generated.
The prototype will share the property.
6. Define objects using prototype + Constructor (attributes of objects do not interfere with each other and share the same method)
FunctionPerson (){
This. Username =NewArray ();
This. Password = "123 ";
}
Person. Prototype. getinfo =Function(){
Alert (This. Username + "," +This. Password );
}
VaRP1 =NewPerson ();
VaRP2 =NewPerson ();
P1.username. Push ("zhangsan ");
P2.username. Push ("Lisi ");
P1.getinfo ();
P2.getinfo ();
7. Dynamic Prototype (in the constructor, all objects share a method with the flag, and each object has its own attributes)
FunctionPerson (){
This. Username = "zhangsan ";
This. Password = "123 ";
If(TypeofPerson. Flag = "undefined "){
Alert ("invoked ");
Person. Prototype. getinfo =Function(){
Alert (This. Username + "," +This. Password );
}
Person. Flag =True;
}
}
VaRP =NewPerson ();
VaRP2 =NewPerson ();
P. getinfo ();
P2.getinfo ();