日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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
一個對象,作為新創(chuàng)建對象的原型。
propertiesObject
可選。該參數(shù)對象是一組屬性與值,該對象的屬性名稱將是新創(chuàng)建的對象的屬性名稱,值是屬性描述符(這些屬性描述符的結(jié)構(gòu)與Object.defineProperties()的第二個參數(shù)一樣)。注意:該參數(shù)對象不能是?undefined,另外只有該對象中自身擁有的可枚舉的屬性才有效,也就是說該對象的原型鏈上屬性是無效的。

拋出異常

如果 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ī)范

規(guī)范版本規(guī)范狀態(tài)注解
ECMAScript 5.1 (ECMA-262)
Object.create
StandardInitial definition. Implemented in JavaScript 1.8.5
ECMAScript 2015 (6th Edition, ECMA-262)
Object.create
Standard?

瀏覽器兼容性

  • Desktop
  • Mobile
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support54.0 (2)911.605

相關(guān)鏈接

    • Object.defineProperty
    • Object.defineProperties
    • Object.prototype.isPrototypeOf
    • John Resig's post on?getPrototypeOf

?

總結(jié)

以上是生活随笔為你收集整理的firefox-Developer开发者站点——关于Object.create()新方法的介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。