Two ways for Javascript to create objects __java

Source: Internet
Author: User
Javascript Object Summary (Create object)

The JavaScript language itself can also be object-oriented programming, as the following is a summary of JavaScript object-oriented programming in recent days.
Creation of objects
There are two ways to create JavaScript objects
1. Use object initializers:
objname = {
Prop1:value_1,
Prop2:value_2,
...
}

This method creates the instance object directly without having to declare it.


2. Use constructors:
such as: Fuction Engineer (PARA1,PARA2) {
This.para1 = PARA1;
This.para2 = Para2;
...
}
My bill = new Engineer ("Bill", "24");

The method needs to create the instance with new ().

Add a new attribute for an object type:
such as: Bill.prototype.sex = "man";
In this way, all objects of the engineer type have attribute sex, whose value is "man",
And the following statement:
Bill.sex = "Man";
You just add a property to the Bill object itself.

Define a method for an object:
function Draw () {
...
}
Fuction Engineer (PARA1,PARA2) {
This.para1 = PARA1;
This.para2 = Para2;
This.draw = Draw;
...
}
My bill = new Engineer ("Bill", "24");
Bill.draw ();
You can also use the following definition method:
objname = {
Prop1:value_1,
Prop2:value_2,
Draw:function (num) {
...
}
...
}

Use Objname.draw () when quoting;

To delete an attribute of an object:
Creates a new, MyObj, with two properties, A and B.
myobj = new Object;
myobj.a=5;
myobj.b=12;

Removes the A property, leaving MyObj and only the "B" property.
Delete myobj.a;//deletes the A property of the MyObj instance object

One way to delete an object:

Delete objname.draw;//deletes the draw function of the Objnmae instance object

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.