JavaScript Object objects

Source: Internet
Author: User
Tags access properties

Object is one of the most used types of ECMAScript. There are two ways to use an object instance. One is with the new operator, and the other is the object literal method.

1.new operator:

1 var person=New  Object (); 2 person.name= "Zhangsan"; 3 person.age=21;

2. Object literal:

1 var person={2  name: "Zhang San",3 age:294 };

In object literals, the object's property name can be used as a string, or it can be written as:

1 var person={2  "name": "Zhang San",3  "age": 4  5:true5  };
In the object literal, use commas to separate different attributes, and the last attribute does not add commas if added in IE7 and earlier versions and opera will error.
You can also write this:
1 var person={};2 person.name= "a"; 3 person.age=29;

When you define an object by object literal, you do not actually call the object constructor.

In general, the object property is accessed using the dot notation, but in JS you can also access the property by using square brackets notation. When square brackets are used, the attributes are placed in square brackets as strings, for example:

1 alert (person["name"]);

There is no difference in functionality, but the advantage of square brackets is that you can access properties by using variables, such as:

1 var a= "name"; 2 alert (Person[a]);
You can also use square brackets if the property name contains a character that causes a syntax error, or if the property name uses a keyword or reserved word.
For example: person["First name"] = "Zhangsan";
Unless you must use square brackets , point notation is recommended.
write at the end: an element in an array is like an object indexed as a property name, an element is a property value, or arr=[' a ', ' B ' is somewhat like a arrobj={0: ' A ', 1: ' B '}, an object whose properties can be passed "." To access it, but using the following will throw a syntax error,
because the property name is illegal:console.log (arr.0);
JavaScript 中数字开头的属性不能跟在点号后面;必须在方括号中使用。
对象可以用[]和.表示;而数组只能用[],表示;

JavaScript Object objects

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.