博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES5 Object.create 方法
阅读量:6998 次
发布时间:2019-06-27

本文共 2421 字,大约阅读时间需要 8 分钟。

Object.create(proto[, propertiesObject])

The Object.create() method creates a new object with the specified prototype object and properties.
第1个参数是该对象的 prototype, 第2个参数和 Object.defineProperties 第2个参数类似

 

var o;// create an object with null as prototypeo = 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// Example where we create an object with a couple of// sample properties. (Note that the second parameter// maps keys to *property descriptors*.)o = Object.create(Object.prototype, {  // foo is a regular 'value property'  foo: {    writable: true,    configurable: true,    value: 'hello'  },  // bar is a getter-and-setter (accessor) property  bar: {    configurable: false,    get: function() { return 10; },    set: function(value) {      console.log('Setting `o.bar` to', value);    }/* with ES5 Accessors our code can look like this    get function() { return 10; },    set function(value) {      console.log('Setting `o.bar` to', value);    } */  }});// Create a new object whose prototype is a new, empty// object and add a 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;// 42o.q = 12;for (var prop in o) {  console.log(prop);}// 'q'delete o.p;// false// to specify an ES3 propertyo2 = Object.create({}, {  p: {    value: 42,    writable: true,    enumerable: true,    configurable: true  }});

 

Polyfill

if (typeof Object.create != 'function') {  Object.create = (function(undefined) {    var Temp = function() {};    return function (prototype, propertiesObject) {      if(prototype !== Object(prototype)) {        throw TypeError(          'Argument must be an object, or null'        );      }      Temp.prototype = prototype || {};      var result = new Temp();      Temp.prototype = null;      if (propertiesObject !== undefined) {        Object.defineProperties(result, propertiesObject);       }             // to imitate the case of Object.create(null)      if(prototype === null) {         result.__proto__ = null;      }       return result;    };  })();}

 

参考地址:

转载于:https://www.cnblogs.com/zhengming2016/p/6718932.html

你可能感兴趣的文章
juniper的一些基本命令
查看>>
cocos2d-x学习之CCDictionary中的一个小问题
查看>>
运维少年系列 - ansible and cisco(2)
查看>>
我的友情链接
查看>>
Android 动画之TranslateAnimation应用详解
查看>>
Android Interactive Animation
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
无聊,用c写了个后台扫描
查看>>
洛谷——P2434 [SDOI2005]区间
查看>>
WDS+MDT+WAIK简单部署Win7之捕捉映像
查看>>
致即将逝去的青春
查看>>
haproxy,能够实现将来自用户的80端口的http请求转发至后端8000上的server服务
查看>>
Windows Server 2008 如何去除“IE 增加的安全配置”
查看>>
Emeditor自动切换到单元格选择模式
查看>>
awk学习
查看>>
我的友情链接
查看>>
mongodb中的副本集搭建实践
查看>>
Javascript 实现形如Extjs中的“关闭并带回”
查看>>
resources.resx相关
查看>>