Object.create()方法
生活随笔
收集整理的這篇文章主要介紹了
Object.create()方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Object.create()方法創建一個新對象,使用現有的對象來提供新創建的對象的proto。
語法:Object.create(proto, [propertiesObject])
- proto:新創建對象的原型對象。必填
- propertiesObject:可選。若沒有指定為undefined,則是要添加到新創建對象的可枚舉屬性(自身定義的屬性,而不是原型鏈上的枚舉屬性。這些屬性對應Object.defineProperties()的第二個參數 。
propertiesObject 參數的詳細解釋:(默認都為false)
數據屬性:
-
writable: 是否可任意寫
-
configurable:是否能夠刪除,是否能夠被修改
-
enumerable:是否能用 for in 枚舉
-
value:值
訪問屬性:
- get()訪問
- set()設置
返回值:一個新對象,帶著指定的原型對象和屬性。
const person = {isable:false, printIntroduction: function () {console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);}}var onePerson = Object.create(person); // onePerson繼承person對象 onePerson.__proto__ === person; // true const person = {isable:false, printIntroduction: function () {console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);}}var onePerson = Object.create(person,{ color:{ writable: true, configurable:true, value: 'red' }, bar: {configurable: false,get: function() { return 10; },set: function(value) {console.log("Setting `o.bar` to", value);}} });總結
以上是生活随笔為你收集整理的Object.create()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: substring、substr以及sl
- 下一篇: 解决在IOS系统及微信中audio、vi