動態原型模式
阿新 • • 發佈:2018-07-06
pre pro 斷開 color code class 指針 name say
function Person1(name,age){ this.name = name; this.age = age; if (typeof this.sayName != ‘function‘) { Person1.prototype.sayName = function(){ console.log(this.name); } } }
function Person1(name,age){ this.name = name; this.age = age; if (typeof this.sayName != ‘function‘) { Person1.prototype = { sayName:function(){ console.log(this.name); } } } }
上邊和下邊不一樣,下邊會斷開以前的原型的指針,因為{}就是一個對象
動態原型模式