面對對象(二)

new.target
函數內部能夠使用new.target屬性。若是當前函數是new命令調用,new.target指向當前函數,不然爲undefined。函數

function f() {
console.log(new.target === f);
}this

f() // false
new f() // true
Object.create() 建立實例對象
構造函數做爲模板,能夠生成實例對象。可是,有時拿不到構造函數,只能拿到一個現有的對象。咱們但願以這個現有的對象做爲模板,生成新的實例對象,這時就能夠使用Object.create()方法。code

var person1 = {
name: '張三',
age: 38,
greeting: function() {對象

console.log('Hi! I\'m ' + this.name + '.');

}
};繼承

var person2 = Object.create(person1);get

person2.name // 張三
person2.greeting() // Hi! I'm 張三.
上面代碼中,對象person1是person2的模板,後者繼承了前者的屬性和方法。io

Object.create()的詳細介紹,請看後面的相關章節。console

相關文章
相關標籤/搜索