创建一个具有指定原型且可选择性地包含指定属性的对象。

语法

Object.create(prototype, descriptors)

参数

prototype

必需。  要用作原型的对象。  可以为 null

descriptors

可选。  包含一个或多个属性描述符的 JavaScript 对象。

“数据属性”是可获取且可设置值的属性。  数据属性描述符包含 value 特性,以及 writableenumerable 和 configurable 特性。  如果未指定最后三个特性,则它们默认为 false。  只要检索或设置该值,“访问器属性”就会调用用户提供的函数。  访问器属性描述符包含 set特性和/或 get 特性。  有关详细信息,请参阅 Object.defineProperty 函数 (JavaScript)

返回值

一个具有指定的内部原型且包含指定的属性(如果有)的新对象

异常

如果满足下列任一条件,则将引发 TypeError 异常:

  • prototype 参数不是对象且不为 null

  • descriptors 参数中的描述符具有 value 或 writable 特性,并具有 get 或 set 特性。

  • descriptors 参数中的描述符具有不为函数的 get 或 set 特性。

  • Exception    Condition

备注

若要停止原型链,可以使用采用了 null prototype 参数的函数。  所创建的对象将没有原型。

示例

下面的示例创建使用 null 原型的对象并添加两个可枚举的属性。

 var newObj = Object.create(null, {
size: {
value: "large",
enumerable: true
},
shape: {
value: "round",
enumerable: true
}
}); document.write(newObj.size + "<br/>");
document.write(newObj.shape + "<br/>");
document.write(Object.getPrototypeOf(newObj)); // Output:
// large
// round
// null

示例

下面的示例创建一个具有与 Object 对象相同的内部原型的对象。  您会发现,该对象具有与使用对象文本创建的对象相同的原型。  Object.getPrototypeOf 函数可获取原始对象的原型。  若要获取对象的属性描述符,可以使用Object.getOwnPropertyDescriptor 函数 (JavaScript)

 var firstLine = { x: undefined, y: undefined };

 var secondLine = Object.create(Object.prototype, {
x: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
},
y: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
}
}); document.write("first line prototype = " + Object.getPrototypeOf(firstLine));
document.write("<br/>");
document.write("second line prototype = " + Object.getPrototypeOf(secondLine)); // Output:
// first line prototype = [object Object]
// second line prototype = [object Object]

示例

下面的示例创建一个具有与 Shape 对象相同的内部原型的对象。

 // Create the shape object.
var Shape = { twoDimensional: true, color: undefined, hasLineSegments: undefined }; var Square = Object.create(Object.getPrototypeOf(Shape));

要求

在以下文档模式中受支持:Internet Explorer 9 标准模式、Internet Explorer 10 标准模式和 Internet Explorer 11 标准模式。 此外,也在应用商店应用(Windows 8 和 Windows Phone 8.1)中受支持。 请参阅版本信息

在以下文档模式中不受支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式。

资料来源:https://msdn.microsoft.com/zh-cn/library/ff925952(v=vs.94).aspx

Object.create 函数 (JavaScript)的更多相关文章

  1. Object.create函数

    创建一个具有指定原型且可选择性地包含指定属性的对象. Object.create(prototype, descriptors) 参数 prototype必需. 要用作原型的对象. 可为 null. ...

  2. Object.keys 函数 (JavaScript)

    Object.keys 函数 (JavaScript) 返回对象的可枚举属性和方法的名称. 在实际开发中,我们有时需要知道对象的所有属性,原生js给我们提供了一个很好的方法:Object.keys() ...

  3. javascript一种新的对象创建方式-Object.create()

    1.Object.create() 是什么? Object.create(proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不 ...

  4. [设计模式] JavaScript 之 原型模式 : Object.create 与 prototype

    原型模式说明 说明:使用原型实例来 拷贝 创建新的可定制的对象:新建的对象,不需要知道原对象创建的具体过程: 过程:Prototype => new ProtoExam => clone ...

  5. javascript Object.create()究竟发生了什么

      这是我在博客园的第一篇博客,早上看了一个大牛的博客,关于javascript继承的,对于大牛使用Object.create()实现继承的方式觉得点问题,就自己研究了一下,所以就有了这篇帖子. 本帖 ...

  6. Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create

    Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...

  7. [Effective JavaScript 笔记]第31条:使用Object.getPrototypeOf函数而不要使用__proto__属性

    ES5引入Object.getPrototypeOf函数作为获取对象原型的标准API,但由于之前的很多js引擎使用了一个特殊的__proto__属性来达到相同的目的.但有些浏览器并不支持这个__pro ...

  8. Object.create() vs new SomeFunction() in javascript

    Object.create builds an object that inherits directly from the one passed as its first argument. Wit ...

  9. [Javascript] Prototype 2 Object.create()

    function Fencepost (x, y, postNum){ this.x = x; this.y = y; this.postNum = postNum; this.connections ...

随机推荐

  1. div里包含img底部必定多出空白的解决办法

    研究了很久,自己写了js代码都解决不了.最后还是靠万能的网友解决了这一问题! 问题:adding.margin.border都设为0,无效.怎么样都多出3px. 解决方案: 1.设置div{ font ...

  2. OLA音频变速算法的仿真与剖析

    前段时间,在尝试音乐节拍数的提取时,终于有了突破性的进展,效果基本上比市面上的许多商业软件还要好,在作节拍数检测时,高频信息作用不大, 通过重采样减小运算量.重采样让我想起了在学校里面做的变速变调算法 ...

  3. 越狱后的ios如何用apt-get 安装各种命令

    越狱后的ios如何用apt-get 安装各种命令   iphone越狱后想玩linux. 1. ssh 客户端:ssh Term Pro. 2. 只装客户端是连不上的,还得一个 ssh connect ...

  4. phpstorm配置代码自动同步到服务器

    首先找到你的菜单栏找到Tools 然后点击配置 填写你的服务器信息 填写好项目目录 选择自动上传

  5. 自动化监控利器-Zabbix

    转自: http://www.xuliangwei.com/xubusi/117.html   1.1为何需要监控系统 在一个IT环境中会存在各种各样的设备,例如:硬件设备.软件设备.其系统的构成也是 ...

  6. Node.js——Async

    一:流程控制 为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程 ...

  7. chrome调试命令模式

    哈哈哈

  8. [转载]能不能同时用static和const修饰类的成员函数?

    题目(一):我们可以用static修饰一个类的成员函数,也可以用const修饰类的成员函数(写在函数的最后表示不能修改成员变量,不是指写在前面表示返回值为常量).请问:能不能同时用static和con ...

  9. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  10. 在java项目中使用AES256 CBC加密

    首先要注意一点,默认的JDK是不支持256位加密的,需要到Oracle官网下载加密增强文件(Java Cryptography Extension (JCE) Unlimited Strength J ...