You define (and create) a JavaScript object with an object literal:
var person = {firstName:"Fatli", lastName:"Lee", age:22, eyeColor:"black"};
// Spaces and line breaks are not important. An object definition can span multiple lines:
var person = {
firstName:"Fatli",
lastName:"Lee",
age:22,
eyeColor:"black"
};
JavaScript 对象
JavaScript 中的所有事物都是对象:字符串、数字、数组、日期、函数,等等。
JavaScript 中,对象是数据(变量),拥有属性和方法。对象是拥有属性和方法的数据。(在面向对象的语言中,属性和方法常被称为对象的成员)
例如:这样声明一个 JavaScript 变量时:
实际上已经创建了一个 JavaScript 字符串对象。字符串对象拥有内建的属性
stringObject.length
和内建的方法stringObject.indexOf(searchvalue,fromindex)
。对象属性:
对象方法:
JavaScript 创建对象
创建名为 "person" 的对象,并为其添加了四个属性:
访问对象属性
or
访问对象方法
实例: