JavaScript quotes
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
属性也可能是函数
相关文章
- JavaScript实现复选框全选与全不选的效果
- JavaScript一个简单的验证页面
- [Javascript] Create scrollable DOM elements with Greensock
- JavaScript原生对象常用方法总结
- 跨浏览器编程之JavaScript
- [Testing] Config jest to test Javascript Application -- Part 1
- [Javascript] JavaScript赋值时的传值与传址
- [Javascript] Use Number() to convert to Number if possilbe
- Javascript模块化编程(二):AMD规范
- 学习Javascript的书籍
- 一段简单的JavaScript代码,模拟Angular数据绑定信息的解析和替换
- 用javascript实现一个stack overflow的例子
- javascript箭头函数
- python web py入门(25)- javascript的除法
- javascript怎么判断字符串中全为f?