概述
An object is very similar to an array but with the difference that you define the keys yourself. You're not limited to using only numeric indexes but can use friendlier keys, such as first_name, age, and so on.
Let's 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};
It's recommended that you don't 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 Object's 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
属性也可能是函数
最后
以上就是光亮百褶裙为你收集整理的JavaScript quotes的全部内容,希望文章能够帮你解决JavaScript quotes所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复