js 原型繼承 聖盃模式
阿新 • • 發佈:2018-11-08
聖盃模式:隔離 - 繼承
// 聖盃模式 var inherit = (function () { function F() {} return function (Obj, Target) { F.prototype = Target.prototype; Obj.prototype = new F(); Obj.prototype.constructor = Obj; Obj.prototype.uber= Target.prototype; } }()); function People() {} function Man() {} People.prototype = { name: 'wj', age: '28', say: function () { console.log(this.age); } } People.prototype.add= function () { this.age++; } inherit(Man, People); var man = new Man(); man.age = 100; console.log(man.age); // 100 man.add(); console.log(man.age); // 101 delete man.age; console.log(man.age); // 28