zl程序教程

您现在的位置是:首页 >  前端

当前栏目

JavaScript quotes

2023-09-14 08:59:40 时间

An object is very similar to an array but with the difference that you define the keys yourself. Youre not limited to using only numeric indexes but can use friendlier keys, such as first_name, age, and so on.
Lets take a look at a simple object and examine its parts:
var hero = {
breed: Turtle,
occupation: Ninja
};
You can see that:
The name of the variable that contains the object is hero
Instead of [ and ] which you use to define an array, you use { and }
for objects
You separate the elements (called properties) contained in the object
with commas
The key/value pairs are divided by colons, as key: value
The keys (names of the properties) can optionally be placed in quotation marks.
For example these are all the same:
var o = {prop: 1};
var o = {"prop": 1};
var o = {prop: 1};
Its recommended that you dont quote the names of the properties (it is also less typing!), but there are some cases when you have must use quotes:
If the property name is one of the reserved words in JavaScript (see Appendix A)
If it contains spaces or special characters (anything other than letters, numbers, and the underscore character)
If it starts with a number

 

 

Accessing Objects Properties
There are two ways to access a property of an object:
Using square bracket notation, for example hero[occupation]
Using the dot notation, for example hero.occupation

属性也可能是函数