js中Object.create實例用法詳解
更新時間:2021年10月05日 08:32:49 作者:小妮淺淺
在本篇文章里小編給大家整理的是一篇關于js中Object.create實例用法及相關基礎內(nèi)容,有興趣的朋友們可以學習下。
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 cookie基礎應用之記錄用戶名的方法
這篇文章主要介紹了javascript cookie基礎應用之記錄用戶名的方法,涉及javascript基于cookie針對數(shù)據(jù)存儲的簡單應用,需要的朋友可以參考下2016-09-09
JavaScript簡介_動力節(jié)點Java學院整理
JavaScript是一種基于對象(Object)和事件驅動(EventDriven)并具有安全性能的腳本語言,javascript的出現(xiàn)使得網(wǎng)頁和用戶之間實現(xiàn)了一種實時性的,動態(tài)性的,交互性的關系,使網(wǎng)頁包含更多活躍的元素和更加精彩的內(nèi)容2017-06-06

