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"
運行結果
> "My name is Matthew. Am I human? true"
到此這篇關于js中Object.create實例用法詳解的文章就介紹到這了,更多相關js中Object.create方法是什么內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
第一個JavaScript入門基礎 document.write輸出
關于JavaScript,他是一個OOSP(面向?qū)ο竽_本語言)他是用來創(chuàng)建動態(tài)網(wǎng)站,增強用戶界面的一門技術。如果你想了解更多關于JavaScript的信息,請去維基百科查詢。2010-02-02基于dom編程中 動態(tài)創(chuàng)建與刪除元素的使用
本篇文章小編將為大家介紹,基于dom編程中動態(tài)創(chuàng)建與刪除元素的使用,有需要的朋友可以參考一下2013-04-04