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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

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

發(fā)布時(shí)間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 firefox-Developer开发者站点——关于Object.create()新方法的介绍 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create

?

Object.create() 方法創(chuàng)建一個(gè)擁有指定原型和若干個(gè)指定屬性的對(duì)象。

語(yǔ)法

Object.create(proto, [ propertiesObject ])

參數(shù)

proto
一個(gè)對(duì)象,作為新創(chuàng)建對(duì)象的原型。
propertiesObject
可選。該參數(shù)對(duì)象是一組屬性與值,該對(duì)象的屬性名稱將是新創(chuàng)建的對(duì)象的屬性名稱,值是屬性描述符(這些屬性描述符的結(jié)構(gòu)與Object.defineProperties()的第二個(gè)參數(shù)一樣)。注意:該參數(shù)對(duì)象不能是?undefined,另外只有該對(duì)象中自身?yè)碛械目擅杜e的屬性才有效,也就是說(shuō)該對(duì)象的原型鏈上屬性是無(wú)效的。

拋出異常

如果 proto 參數(shù)不是 null 或一個(gè)對(duì)象值,則拋出一個(gè) TypeError 異常。

例子

使用Object.create實(shí)現(xiàn)類式繼承

下面的例子演示了如何使用Object.create()來(lái)實(shí)現(xiàn)類式繼承。這是一個(gè)單繼承。

//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."

如果你希望能繼承到多個(gè)對(duì)象,則可以使用混入的方式。

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ù)會(huì)把超類原型上的函數(shù)拷貝到子類原型上,這里mixin函數(shù)沒(méi)有給出,需要由你實(shí)現(xiàn)。一個(gè)和 mixin 很像的函數(shù)是??jQuery.extend。

使用Object.create 的 propertyObject 參數(shù)

var o;// 創(chuàng)建一個(gè)原型為null的空對(duì)象 o = Object.create(null); o = {}; // 以字面量方式創(chuàng)建的空對(duì)象就相當(dāng)于: o = Object.create(Object.prototype); o = Object.create(Object.prototype, { // foo會(huì)成為所創(chuàng)建對(duì)象的數(shù)據(jù)屬性 foo: { writable:true, configurable:true, value: "hello" }, // bar會(huì)成為所創(chuàng)建對(duì)象的訪問(wèn)器屬性 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)建一個(gè)以另一個(gè)空對(duì)象為原型,且擁有一個(gè)屬性p的對(duì)象 o = Object.create({}, { p: { value: 42 } }) // 省略了的屬性特性默認(rèn)為false,所以屬性p是不可寫(xiě),不可枚舉,不可配置的: o.p = 24 o.p //42 o.q = 12 for (var prop in o) { console.log(prop) } //"q" delete o.p //false //創(chuàng)建一個(gè)可寫(xiě)的,可枚舉的,可配置的屬性p o2 = Object.create({}, { p: { value: 42, writable: true, enumerable: true, configurable: true } });

Polyfill

本polyfill的實(shí)現(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è)共享的構(gòu)造器 function Temp() {} // 使用 Object.prototype.hasOwnProperty 更安全的引用 var hasOwn = Object.prototype.hasOwnProperty; return function (O) { // 1. 如果 O 不是 Object 或 null,拋出一個(gè) TypeError 異常。 if (typeof O != 'object') { throw TypeError('Object prototype may only be an Object or null'); } // 2. 使創(chuàng)建的一個(gè)新的對(duì)象為 obj ,就和通過(guò) // new Object() 表達(dá)式創(chuàng)建一個(gè)新對(duì)象一樣, // Object是標(biāo)準(zhǔn)內(nèi)置的構(gòu)造器名 // 3. 設(shè)置 obj 的內(nèi)部屬性 [[Prototype]] 為 O。 Temp.prototype = O; var obj = new Temp(); Temp.prototype = null; // 不要保持一個(gè) O 的雜散引用(a stray reference)... // 4. 如果存在參數(shù) Properties ,而不是 undefined , // 那么就把參數(shù)的自身屬性添加到 obj 上,就像調(diào)用 // 攜帶obj ,Properties兩個(gè)參數(shù)的標(biāo)準(zhǔn)內(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()新方法的介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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