學(xué)習(xí)心得:
// 2. 原型和原型鏈
// 方式: 1,2, 3
/**
?* 1. 一句話:萬物結(jié)對象,萬物皆空 (對象最終會指向null)
?* 2. 兩個定義: 原型:保存所有子對象的共有屬性值和方法的父對象
?* 原型鏈:由各級子對象的__proto__的屬性連接引用形成的結(jié)構(gòu)
?* 3. 三個屬性:__proto__、contructor、prototype
?* 4. 構(gòu)造函數(shù)實現(xiàn)類
?*?
?*?
?*?
?*?
?*/
//1. 當(dāng)函數(shù)創(chuàng)建時,就會帶上一個prototype屬性,這個屬性指向prototype對象,也就是原型對象
Person.prototype.money = 20000
Person.prototype.run = function() {
? ? console.log('跑步')
}
Person.prototype.run()
//contructor: Person.prototype 攜帶
console.log(Person.prototype.constructor===Person)
let p1 = new Person('張三', 18)
let p2 = new Person('李四',20)
//2. p1.__proto__: js 所有對象都會攜帶Array Function Date...
console.log(p1.__proto__ === Person.prototype)