Object.create 函数 (JavaScript)
创建一个具有指定原型且可选择性地包含指定属性的对象。
- Object.create(prototype, descriptors)
prototype
必需。 要用作原型的对象。 可以为 null。
descriptors
可选。 包含一个或多个属性描述符的 JavaScript 对象。
“数据属性”是可获取且可设置值的属性。 数据属性描述符包含 value 特性,以及 writable、enumerable 和 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)的更多相关文章
- Object.create函数
创建一个具有指定原型且可选择性地包含指定属性的对象. Object.create(prototype, descriptors) 参数 prototype必需. 要用作原型的对象. 可为 null. ...
- Object.keys 函数 (JavaScript)
Object.keys 函数 (JavaScript) 返回对象的可枚举属性和方法的名称. 在实际开发中,我们有时需要知道对象的所有属性,原生js给我们提供了一个很好的方法:Object.keys() ...
- javascript一种新的对象创建方式-Object.create()
1.Object.create() 是什么? Object.create(proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不 ...
- [设计模式] JavaScript 之 原型模式 : Object.create 与 prototype
原型模式说明 说明:使用原型实例来 拷贝 创建新的可定制的对象:新建的对象,不需要知道原对象创建的具体过程: 过程:Prototype => new ProtoExam => clone ...
- javascript Object.create()究竟发生了什么
这是我在博客园的第一篇博客,早上看了一个大牛的博客,关于javascript继承的,对于大牛使用Object.create()实现继承的方式觉得点问题,就自己研究了一下,所以就有了这篇帖子. 本帖 ...
- Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create
Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...
- [Effective JavaScript 笔记]第31条:使用Object.getPrototypeOf函数而不要使用__proto__属性
ES5引入Object.getPrototypeOf函数作为获取对象原型的标准API,但由于之前的很多js引擎使用了一个特殊的__proto__属性来达到相同的目的.但有些浏览器并不支持这个__pro ...
- Object.create() vs new SomeFunction() in javascript
Object.create builds an object that inherits directly from the one passed as its first argument. Wit ...
- [Javascript] Prototype 2 Object.create()
function Fencepost (x, y, postNum){ this.x = x; this.y = y; this.postNum = postNum; this.connections ...
随机推荐
- Ubuntu 16.04播放器Rhythmbox乱码解决
使用Rhythmbox进行音乐播放的时候,歌曲名称专辑歌手名称都出现乱码,查看了网上很多教程,要不就是将音频转码,要不就是修改用户环境编码配置.前一种方法对音频有改动,后一种可能无效还有可能会影响系统 ...
- YUVviewerPlus使用教程
1.YUVviewerPlus用于播放yuv文件,点击Open File打开yuv文件 2.点击Play播放yuv文件
- 【Java MyBatis Generator】使用generator自动生成Dao,Mapping和实体文件
具体请参照: http://blog.csdn.net/fengshizty/article/details/43086833 按照上面博客地址,下载Generator的依赖包: 如下是我的配置文件: ...
- Linux CAT与ECHO命令详解 <<EOF EOF
Linux CAT与ECHO命令详解 cat命令是Linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: .一次显示整个文件. $ cat filename .从键盘 ...
- mysql同一台服务器上不同数据库中个别表内容同步
>>>>>>soft_wsx>>>>>>--数据备份与还原>>同步备用服务器--1.完全备份主数据库--2.使用带S ...
- jq鼠标隐藏显示的方法
<div style="width:300px; float:left;"> <div onmouseover="testOnmouseO ...
- mysql删除二进制日志文件
一.RESET MASTER 这个语句可以验证首次配置主机备机是否成功.步骤如下: 1. 启动master和 slave,开启replication (即 复制) 注:replication (复制) ...
- 几种Linux 查询外网出口IP的方法
Curl 纯文本格式输出: curl icanhazip.com curl ifconfig.me curl curlmyip.com curl ip.appspot.com curl ipinfo. ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...