1、構(gòu)造函數(shù)
function Person(){
}
2、創(chuàng)建實例
const person = new Person();
3、原型對象
Person.prototype.name = 'ivy';
Person.prototype.will = 'become excellent';
4、三者關(guān)系
(1)、只要創(chuàng)建了一個新函數(shù),就會根據(jù)一組特定的規(guī)則為該函數(shù)創(chuàng)建一個prototype屬性。這個屬性指向函數(shù)的原型對象;
(2)、所有原型對象都會獲得constructor屬性,這個屬性指向prototype屬性所在函數(shù)的指針,即構(gòu)造函數(shù)
(3)、實例對象有屬性proto指向原型對象(only firefox、safary、chrome support)
5、isPrototypeOf()
Person.prototype.isPrototypeOf(person) // return true
6、Object.getPrototypeOf()
該方法返回實例原型對象
Object.getPrototypeOf(person) === Person.prototype
7、hasOwnProperty()
該方法檢測一個屬性是否存在于實例中
person.hasOwnProperty('name')
8、原型與in操作符
檢測屬性是否存在于原型中或?qū)嵗?/p>
9、hasPrototypeProperty()
該方法檢測屬性是否存在于原型中
10、更簡單的原型語法(對象字面量的形式)
Person.prototype = {
name: 'ivy',
age: '25',
will: 'become excellent'
}