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

ES5 Object.create 方法的更多相关文章

  1. 怎样理解Object.create()方法

    Object.create()是一个用于生成新的对象的方法, 特点是: 1. Object.create()接收的第一个参数对象将会作为待生成的新对象的原型对象; 2. Object.create() ...

  2. 怎样手写一个Object.create()方法

    Object.create()会将参数对象作为一个新创建的空对象的原型, 并返回这个空对象, 基于这个功能, 就有了下面这个Object.create()的手动实现: function _create ...

  3. 关于Object.create方法

    ES6最新的Object.create语法是 创造一个对象 可以传参,参数为一个对象,得到的结果是一个克隆的对象, 实际上 这是基于原型的克隆 分析如下: var a={b:1}; var a1 = ...

  4. Object.create()方法的低版本兼容问题

    Object.prototype.create=(function(){ if(Object.prototype.create){return Object.prototype.create}else ...

  5. ES5 Object.defineProperty 方法

    先看一个例子: var o = {}; o.a = 1; // 等待于: Object.defineProperty(o, 'a', { value: 1, writable: true, confi ...

  6. firefox-Developer开发者站点——关于Object.create()新方法的介绍

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create Objec ...

  7. javascript ES5 Object对象

    原文:http://javascript.ruanyifeng.com/stdlib/object.html 目录 概述 Object对象的方法 Object() Object.keys(),Obje ...

  8. Object的方法

    1.Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. ES2015引入的 ,且可用polyfilled.要支持旧浏览器的话,可用使用jQ ...

  9. js学习日记-new Object和Object.create到底干了啥

    function Car () { this.color = "red"; } Car.prototype.sayHi=function(){ console.log('你好') ...

随机推荐

  1. android:process结合activity启动模式的一次实践

    会有这样的场景,一个应用崩溃了,而导致的该应用崩溃的原因是,该应用占用的内存大小超过了系统分配给它的最大堆大小.对象的分配,是发生在堆(heap)上面的,系统分配给每个应用的最大堆大小是固定的. 假设 ...

  2. 【Foreign】Uria [欧拉函数]

    Uria Time Limit: 20 Sec  Memory Limit: 512 MB Description 从前有个正整数 n. 对于一个正整数对 (a,b),如果满足 a + b ≤ n 且 ...

  3. 省队集训Day1 过河

    [题目大意] 小奇特别喜欢猪,于是他养了$n$只可爱的猪,但这些猪被魔法猪教会了魔法,一不看着某些猪就会自己打起来. 小奇要带着他的猪讨伐战狂,路途中遇到了一条河.小奇找到了一条船,可惜这条船一次只能 ...

  4. Bzoj2832 / Bzoj3874 宅男小C

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 124  Solved: 26 Description 众所周知,小C是个宅男,所以他的每天的食物要靠外 ...

  5. 子div设置margin-top使得父div也跟着向下移动

    之前在写网页的时候,发现一个小问题,就是子div设置margin-top的时候,父的div也会跟着向下移动.我用代码和图描述一下问题: <span style="font-size:1 ...

  6. hdu1002 A + B Problem II(大数题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...

  7. Android跳转到拨打电话的页面

    在Android6.0之后,拨打电话需要用户授予动态权限,项目中有此需求,有一种简单的方法,直接携带电话号码跳转到系统拨打电话的页面,很多应用也是这么做的,这样可以减轻工作量 代码如下: Androi ...

  8. 更改控件中DrawableLeft图片的大小,图片与文字的距离

    Drawable drawable=getResources().getDrawable(R.drawable.xx); //获取图片 drawable.setBounds(left, top, ri ...

  9. ribbon使用eureka的meta进行动态路由

    序 使用eureka的元数据信息,再配上ribbon的路由功能,就可以在api-gateway实现很多功能,比如灰度测试.生产调试等等.下面介绍一下,怎么使用jmnarloch大神提供的ribbon- ...

  10. hdfs文件上传机制与namenode元数据管理机制

    1.hdfs文件上传机制 文件上传过程:   1.客户端想NameNode申请上传文件, 2.NameNode返回此次上传的分配DataNode情况给客户端 3.客户端开始依向dataName上传对应 ...