您的位置:首页 > 移动开发 > Objective-C

Object.create

2014-04-03 20:57 232 查看
var emptyObject = Object.create(null);


var emptyObject = Object.create(null);

var emptyObject = {};

var emptyObject = new Object();

区别:

var o;

// create an object with null as prototype
o = Object.create(null);

o = {};
// is equivalent to:
o = Object.create(Object.prototype);

function Constructor(){}
o = new Constructor();
// is equivalent to:
o = Object.create(Constructor.prototype);
// Of course, if there is actual initialization code in the Constructor function, the Object.create cannot reflect it

// create a new object whose prototype is a new, empty object
// and a adding single property 'p', with value 42
o = Object.create({}, { p: { value: 42 } })

// by default properties ARE NOT writable, enumerable or configurable:
o.p = 24
o.p
//42

o.q = 12
for (var prop in o) {
console.log(prop)
}
//"q"

delete o.p
//false

//to specify an ES3 property
o2 = Object.create({}, { p: { value: 42, writable: true, enumerable: true, configurable: true } });


https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐