阅读背景:

圣杯模式来实现对象继承(企业级的继承方式)

来源:互联网 
/* * function: inherit -> 通过圣杯模式来实现继承 * params: * Target:继承的对象 * Origin: 被继承的对象 */ function inherit(Target, Origin) { // careate a buffer function function Buffer() { } // let the prototype of Buffer point to the instance of Origin Buffer.prototype = Origin.prototype // let the prototype of Target point to the instance of Buffer Target.prototype = new Buffer() // rewrite the constructor Target.prototype.construtor = Target // set the superclass Target.prototype.super_class = Origin } function Teacher(){ } // 设置原型上的属性 Teacher.prototype.name = "zhangsan" function Student(){ } // 调用函数来实现继承 inherit(Student, Teacher) var s = new Student() s.age = 18 console.log(s); console.log(s.name); // zhangsan console.log(s.age); // 18 /* * function: inherit -> 通过圣杯模式来实现继承 * params: *



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: