
上QQ阅读APP看书,第一时间看更新
2.2.5 对象类型
和数组一样,对象也是数据的集合,对象也可以保存各种不同类型的数据。但是不同于数组的是,对象是用名称和数值成对的方式来存取数据的,并不是通过下标来访问数据。每个数据被赋予一个名称,这个名称被称为对象的属性,JavaScript通过对象的属性名来存取这些数据。在声明对象时,用“属性名:值”的方式来给属性赋值,赋值使用“:”而不是“=”,每个属性之间用逗号隔开,例如:


调用时可以用“.”来调用对象的属性,也可以直接通过对象名输出全部属性,例如:

输出如图2.15所示。

图2.15 样例输出2.15
对象也可以存取函数,存放在对象中的函数称为对象的方法,同样可以通过调用对象的方法名来调用函数。具体的内容以及有关对象的其他内容,将在后面的章节中深入讲解。