js中Object.create實例用法詳解
1、用Object.create()方法創(chuàng)建新對象,并使用現(xiàn)有對象提供新對象的proto。
2、提供兩個參數(shù),第一個是新創(chuàng)建的原型對象,第二個是為新創(chuàng)建的對象添加屬性的對象。
實例
// father 對象 let father = { name: 'father', friend: ['abby', 'bob'] } // 生成新實例對象 child1 let child1 = Object.create(father) // 更改值類型屬性 child1.name = '修改了name' console.log(child1.name) //修改了name // 更改引用類型值 child1.friend.push('chely') console.log(child1.friend) //[ 'abby', 'bob', 'chely' ] // 生成新實例對象 child2 let child2 = Object.create(father) console.log(child2.name) //father console.log(child2.friend) //[ 'abby', 'bob', 'chely' ]
知識點擴展:
Object.create()創(chuàng)建方法實例
const person = { isHuman: false, printIntroduction: function() { console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`); } }; const me = Object.create(person); me.name = 'Matthew'; // "name" is a property set on "me", but not on "person" me.isHuman = true; // inherited properties can be overwritten me.printIntroduction(); // expected output: "My name is Matthew. Am I human? true"
運行結(jié)果
> "My name is Matthew. Am I human? true"
到此這篇關(guān)于js中Object.create實例用法詳解的文章就介紹到這了,更多相關(guān)js中Object.create方法是什么內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
原生javascript 學(xué)習(xí)之js變量全面了解
下面小編就為大家?guī)硪黄鷍avascript 學(xué)習(xí)之js變量全面了解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07第一個JavaScript入門基礎(chǔ) document.write輸出
關(guān)于JavaScript,他是一個OOSP(面向?qū)ο竽_本語言)他是用來創(chuàng)建動態(tài)網(wǎng)站,增強用戶界面的一門技術(shù)。如果你想了解更多關(guān)于JavaScript的信息,請去維基百科查詢。2010-02-02javascript數(shù)組去重方法總結(jié)(推薦)
這篇文章主要介紹了javascript數(shù)組去重方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03基于dom編程中 動態(tài)創(chuàng)建與刪除元素的使用
本篇文章小編將為大家介紹,基于dom編程中動態(tài)創(chuàng)建與刪除元素的使用,有需要的朋友可以參考一下2013-04-04