ECMAScript新特性【一】--Object.create
Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象
参数:
prototype 必需。 要用作原型的对象。 可以为 null。
descriptors 可选。 包含一个或多个属性描述符的 JavaScript 对象。
“数据属性”是可获取且可设置值的属性。 数据属性描述符包含 value 特性,以及 writable、enumerable 和 configurable 特性。
如果未指定最后三个特性,则它们默认为 false。 只要检索或设置该值,“访问器属性”就会调用用户提供的函数。 访问器属性描述符包含 set 特性和/或 get 特性。
var pt = {
say : function(){
console.log('saying!');
}
} var o = Object.create(pt); console.log('say' in o); // true
console.log(o.hasOwnProperty('say')); // false
如果prototype传入的是null,创建一个没有原型链的空对象。
var o1 = Object.create(null);
console.dir(o1); // object[ No Properties ]
当然,可以创建没有原型链的但带descriptors的对象;
var o2 = Object.create(null, {
size: {
value: "large",
enumerable: true
},
shape: {
value: "round",
enumerable: true
}
}); console.log(o2.size); // large
console.log(o2.shape); // round
console.log(Object.getPrototypeOf(o2)); // null
也可以创建带属性带原型链的对象:
var pt = {
say : function(){
console.log('saying!');
}
} var o3 = Object.create(pt, {
size: {
value: "large",
enumerable: true
},
shape: {
value: "round",
enumerable: true
}
}); console.log(o3.size); // large
console.log(o3.shape); // round
console.log(Object.getPrototypeOf(o3)); // {say:...}
最重要的是实现继承,看下面实例:
//Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
} Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info("Shape moved.");
}; // Rectangle - subclass
function Rectangle() {
Shape.call(this); //call super constructor.
} Rectangle.prototype = Object.create(Shape.prototype); var rect = new Rectangle(); console.log(rect instanceof Rectangle); //true.
console.log(rect instanceof Shape); //true. rect.move(); //"Shape moved."
不支持浏览器的兼容实现:
1、简单实现,也是最常见的实现方式,没有实现第二个参数的功能:
if (!Object.create) {
Object.create = function (o) {
if (arguments.length > 1) {
throw new Error('Object.create implementation only accepts the first parameter.');
}
function F() {}
F.prototype = o;
return new F();
};
}
2、复杂实现,实现第二个参数的大部分功能:
if (!Object.create) { // Contributed by Brandon Benvie, October, 2012
var createEmpty;
var supportsProto = Object.prototype.__proto__ === null;
if (supportsProto || typeof document == 'undefined') {
createEmpty = function () {
return { "__proto__": null };
};
} else {
createEmpty = function () {
var iframe = document.createElement('iframe');
var parent = document.body || document.documentElement;
iframe.style.display = 'none';
parent.appendChild(iframe);
iframe.src = 'javascript:';
var empty = iframe.contentWindow.Object.prototype;
parent.removeChild(iframe);
iframe = null;
delete empty.constructor;
delete empty.hasOwnProperty;
delete empty.propertyIsEnumerable;
delete empty.isPrototypeOf;
delete empty.toLocaleString;
delete empty.toString;
delete empty.valueOf;
empty.__proto__ = null; function Empty() {}
Empty.prototype = empty;
// short-circuit future calls
createEmpty = function () {
return new Empty();
};
return new Empty();
};
} Object.create = function create(prototype, properties) { var object;
function Type() {} // An empty constructor. if (prototype === null) {
object = createEmpty();
} else {
if (typeof prototype !== "object" && typeof prototype !== "function") { throw new TypeError("Object prototype may only be an Object or null"); // same msg as Chrome
}
Type.prototype = prototype;
object = new Type(); object.__proto__ = prototype;
} if (properties !== void 0) {
Object.defineProperties(object, properties);
} return object;
};
}
参考:
https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Object/create
https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Object/defineProperty
https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Object/defineProperties
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
https://github.com/kriskowal/es5-shim/blob/master/es5-sham.js
ECMAScript新特性【一】--Object.create的更多相关文章
- 前端开发者进阶之ECMAScript新特性--Object.create
前端开发者进阶之ECMAScript新特性[一]--Object.create Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指 ...
- 前端开发者进阶之ECMAScript新特性【一】--Object.create
Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数:prototype 必需. 要用作原型的对象. 可以为 nul ...
- ECMAScript5新特性之Object.isExtensible、Object.preventExtensions
阻止对象扩展后: 1 不能添加属性. 2 可以修改属性的值. 3 可以删除属性. 4 可以修改属性描述符. var fruit = { name : '苹果', desc : '红富士' }; // ...
- firefox-Developer开发者站点——关于Object.create()新方法的介绍
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create Objec ...
- Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create
Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...
- 使用 Object.create 创建对象,super 关键字,class 关键字
ECMAScript 5 中引入了一个新方法:Object.create().可以调用这个方法来创建一个新对象.新对象的原型就是调用 create 方法时传入的第一个参数: var a = {a: 1 ...
- ES6、7、8常用新特性总结(超实用)
ES6常用新特性 1. let && const let 命令也用于变量声明,但是作用域为局部 { let a = 10; var b = 1; } 在函数外部可以获取到b,获取不到a ...
- ECMAScript 5和ECMAScript6的新特性以及浏览器支持情况
ECMAScript简介: 它是一种由Ecma国际(前身为欧洲计算机制造商协会)制定和发布的脚本语言规范,javascript在它基础上经行了自己的封装.但通常来说,术语ECMAScript和java ...
- ECMAScript 2017(ES8)新特性简介
目录 简介 Async函数 共享内存和原子操作 Object的新方法 String的新方法 逗号可以添加到函数的参数列表后面了 简介 ES8是ECMA协会在2017年6月发行的一个版本,因为是ECMA ...
随机推荐
- python部分内容存档
笨办法学python. 1 Ec6字符串和文本... 1 ec7. 1 ec8. 1 Ec9. 1 Ec10 转义字符... 1 Ec11提问... 1 raw_input和input的区别... 1 ...
- <node.js爬虫>制作教程
前言:最近想学习node.js,突然在网上看到基于node的爬虫制作教程,所以简单学习了一下,把这篇文章分享给同样初学node.js的朋友. 目标:爬取 http://tweixin.yueyishu ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- 解决引入keras后出现的Using TensorFlow backend的错误
在引入头文件之后,加入 import os os.environ['KERAS_BACKEND']='tensorflow' 就可以完美解决这个问题
- Linux修复文件系统
注意:修复有风险,操作需谨慎.风险一定要说明啊! 由于还没遇到过,我就当网上找了一张图. 如果在启动时,出现了如上图红色框内的RUN fsck MANUALLY,那么一般是文件系统有问题. 最下面提示 ...
- oracle date 看时间
SELECT to_char(DATE_TIME,'yyyy-MM-dd HH24:mi:ss') FROM AUDIT_EVENT;
- 抓包分析LVS-NAT中出现的SYN_RECV
CIP:192.168.10.193 VIP:192.168.10.152:8000 DIP:100.10.8.152:8000 RIP:100.10.8.101:8000 和 100.10.8.10 ...
- Tomcat在Eclips中的使用及注意细节
1.运行环境,先配置Eclips Eclips中的Windows→ preferences→弹出框左边Server→Runtime Environments→右边Add添加需要的Apach Tomca ...
- Wireshark数据抓包教程之Wireshark捕获数据
Wireshark数据抓包教程之Wireshark捕获数据 Wireshark抓包方法 在使用Wireshark捕获以太网数据,可以捕获分析到自己的数据包,也可以去捕获同一局域网内,在知道对方IP地址 ...
- nyoj 211&&poj 3660
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, ...