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 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 // 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;
// 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
}
});

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://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

最新文章

  1. css初始化代码
  2. 二分K-means算法
  3. 浅析C语言指针问题
  4. ecshop /flow.php SQL Injection Vul
  5. rz和sz上传下载文件工具lrzsz
  6. Monte Carlo Approximations
  7. 利用LRUMap 设计缓存
  8. WebMethod 属性
  9. iOS 获取高速随机路径sandbox目录
  10. BFS and Queue
  11. Swift 动态创建提示框
  12. PHP基础知识1
  13. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十二 || 三种跨域方式比较,DTOs(数据传输对象)初探
  14. install svn server in Ubuntu
  15. SQL SERVER中DBLINK的实现
  16. MyBatis基础入门《十八》动态SQL(if-where)
  17. vue.js 常用组件库
  18. Python中则正则表达式
  19. php版本CKFinder3.4.4自定义上传文件位置
  20. ring0 根据EThread遍历线程

热门文章

  1. XAMPP 启动mysql报错 InnoDB: Error: could not open single-table tablespace file……
  2. Android中Handler导致的内存泄露
  3. react-native中使用Echarts,自己使用WebView封装Echarts经验
  4. Linux进程调度与源码分析(三)——do_fork()的实现原理
  5. sk_buff结构
  6. Linux内核中内存cache的实现【转】
  7. 跟踪内核启动过程CONFIG_DEBUG_LL【转自】
  8. tiny-rtems-src
  9. input标签获取焦点时文本框内提示信息清空背景颜色发生变化
  10. jstorm系列-2:入门