JavaScript creates untyped objects using curly braces ({}) syntax

Source: Internet
Author: User

In the traditional object-oriented language, each object will correspond to a class. And the previous section is talking about the this pointer,
Objects in JavaScript are actually a collection of attributes (methods) that have no concept of a strictly meaningful class. So
It provides another simple way to create an object, the curly braces ({}) Syntax:
{
Property1:statement,
Property2:statement2,
...,
Propertyn:statmentn
}
To implement an object by enclosing multiple properties or methods and their definitions (these properties or methods are separated by commas)
Definition, this code directly defines an object with N attributes or methods, where the property name and its definition are
Separated by a colon (:). For example:
<script language= "JavaScript" type= "Text/javascript" >
<!--
var obj={}; An empty object was defined
var user={
Name: "Jack",//defines the name attribute, initialized to Jack
favoritecolor:["Red", "green", "black" and "white"],//defines the array of color preferences
Hello:function () {//defines the method hello
Alert ("Hello," +this.name);
},
Sex: "Male"//defines the gender attribute sex, initialized to sex
}
method to invoke the user object Hello
User.hello ();
-->
</script>
The first line defines an object obj with no type, which is equivalent to:
var obj=new Object ();
It then defines an object user and its properties and methods. Note that, in addition to the last attribute (method) definition,
The other must end with a comma (,). In fact, the method of dynamically adding and subtracting properties can also define a completely identical
User object, the reader may wish to make an attempt using the method described earlier.
You define objects in this way, and you can also use strings as property (method) names, such as:
var obj={"001": "ABC"}

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.