Javascript definition object

Source: Internet
Author: User

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 ();

 

 

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.