在js中,对象就是键值对(就像在ruby中的hash,pythong中的字典),比如:
var myName = {firstName:'Yehuda',lastName:'Katz'}
为了创建一个最简单的对象,你可以这样玩:
var my = Object.create(null)
我们可以通过Object.defineProperty来为对象添加属性:
var person = Object.create(null);
Object.defineProperty(person,'firstName',{
value:'Yehuda',
writable:true,
enumerable:true,
configurable:true
});
当然了,ES5中还实现了类似ember.js中的计算后属性:
var person = Object.create(null);
defineProperty(person,'fullName',function(){return this.firstName + this.lastName;});