Object.create

本文详细比较了JavaScript中创建空对象的不同方法及其内部实现原理,包括使用Object.create(null)与其他方式的区别,深入探讨了原型链及属性配置的影响。
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 } });
复制代码

 

本文转自艾伦 Aaron博客园博客,原文链接:http://www.cnblogs.com/aaronjs/p/3643828.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值