firefox-Developer开发者站点——关于Object.create()新方法的介绍
生活随笔
收集整理的這篇文章主要介紹了
firefox-Developer开发者站点——关于Object.create()新方法的介绍
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create
?
Object.create() 方法創(chuàng)建一個擁有指定原型和若干個指定屬性的對象。
語法
Object.create(proto, [ propertiesObject ])參數(shù)
proto拋出異常
如果 proto 參數(shù)不是 null 或一個對象值,則拋出一個 TypeError 異常。
例子
使用Object.create實現(xiàn)類式繼承
下面的例子演示了如何使用Object.create()來實現(xiàn)類式繼承。這是一個單繼承。
//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(); rect instanceof Rectangle //true. rect instanceof Shape //true. rect.move(1, 1); //Outputs, "Shape moved."如果你希望能繼承到多個對象,則可以使用混入的方式。
function MyClass() { SuperClass.call(this); OtherSuperClass.call(this); } MyClass.prototype = Object.create(SuperClass.prototype); //inherit mixin(MyClass.prototype, OtherSuperClass.prototype); //mixin MyClass.prototype.myMethod = function() { // do a thing };mixin函數(shù)會把超類原型上的函數(shù)拷貝到子類原型上,這里mixin函數(shù)沒有給出,需要由你實現(xiàn)。一個和 mixin 很像的函數(shù)是??jQuery.extend。
使用Object.create 的 propertyObject 參數(shù)
var o;// 創(chuàng)建一個原型為null的空對象 o = Object.create(null); o = {}; // 以字面量方式創(chuàng)建的空對象就相當(dāng)于: o = Object.create(Object.prototype); o = Object.create(Object.prototype, { // foo會成為所創(chuàng)建對象的數(shù)據(jù)屬性 foo: { writable:true, configurable:true, value: "hello" }, // bar會成為所創(chuàng)建對象的訪問器屬性 bar: { configurable: false, get: function() { return 10 }, set: function(value) { console.log("Setting `o.bar` to", value) } }}) function Constructor(){} o = new Constructor(); // 上面的一句就相當(dāng)于: o = Object.create(Constructor.prototype); // 當(dāng)然,如果在Constructor函數(shù)中有一些初始化代碼,Object.create不能執(zhí)行那些代碼 // 創(chuàng)建一個以另一個空對象為原型,且擁有一個屬性p的對象 o = Object.create({}, { p: { value: 42 } }) // 省略了的屬性特性默認為false,所以屬性p是不可寫,不可枚舉,不可配置的: o.p = 24 o.p //42 o.q = 12 for (var prop in o) { console.log(prop) } //"q" delete o.p //false //創(chuàng)建一個可寫的,可枚舉的,可配置的屬性p o2 = Object.create({}, { p: { value: 42, writable: true, enumerable: true, configurable: true } });Polyfill
本polyfill的實現(xiàn)基于Object.prototype.hasOwnProperty。
if (typeof Object.create != 'function') { // Production steps of ECMA-262, Edition 5, 15.2.3.5 // Reference: http://es5.github.io/#x15.2.3.5 Object.create = (function() { //為了節(jié)省內(nèi)存,使用一個共享的構(gòu)造器 function Temp() {} // 使用 Object.prototype.hasOwnProperty 更安全的引用 var hasOwn = Object.prototype.hasOwnProperty; return function (O) { // 1. 如果 O 不是 Object 或 null,拋出一個 TypeError 異常。 if (typeof O != 'object') { throw TypeError('Object prototype may only be an Object or null'); } // 2. 使創(chuàng)建的一個新的對象為 obj ,就和通過 // new Object() 表達式創(chuàng)建一個新對象一樣, // Object是標準內(nèi)置的構(gòu)造器名 // 3. 設(shè)置 obj 的內(nèi)部屬性 [[Prototype]] 為 O。 Temp.prototype = O; var obj = new Temp(); Temp.prototype = null; // 不要保持一個 O 的雜散引用(a stray reference)... // 4. 如果存在參數(shù) Properties ,而不是 undefined , // 那么就把參數(shù)的自身屬性添加到 obj 上,就像調(diào)用 // 攜帶obj ,Properties兩個參數(shù)的標準內(nèi)置函數(shù) // Object.defineProperties() 一樣。 if (arguments.length > 1) { // Object.defineProperties does ToObject on its first argument. var Properties = Object(arguments[1]); for (var prop in Properties) { if (hasOwn.call(Properties, prop)) { obj[prop] = Properties[prop]; } } } // 5. 返回 obj return obj; }; })(); }規(guī)范
| ECMAScript 5.1 (ECMA-262) Object.create | Standard | Initial definition. Implemented in JavaScript 1.8.5 |
| ECMAScript 2015 (6th Edition, ECMA-262) Object.create | Standard | ? |
瀏覽器兼容性
- Desktop
- Mobile
| Basic support | 5 | 4.0 (2) | 9 | 11.60 | 5 |
相關(guān)鏈接
- Object.defineProperty
- Object.defineProperties
- Object.prototype.isPrototypeOf
- John Resig's post on?getPrototypeOf
?
總結(jié)
以上是生活随笔為你收集整理的firefox-Developer开发者站点——关于Object.create()新方法的介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019淘宝拍立淘拍猫猫怎么拍?拍立淘拍
- 下一篇: SharePoint 使用脚本为表单绑定