[JavaScript] 寄生函数 →→→→→进入此内容的聊天室

来自 , 2020-04-08, 写在 JavaScript, 查看 114 次.
URL http://www.code666.cn/view/36366388
  1. //寄生函数
  2. function create(box, desk) {//继承私有的属性和prototype中的属性
  3.         var f = obj(box.prototype);
  4.         f.constructor = desk;                           //调整原型构造指针
  5.         desk.prototype = f;
  6. }
  7.  
  8. function Box(name, age) {
  9.         this.name = name;
  10.         this.age = age;
  11. }
  12.  
  13. Box.prototype.run = function () {
  14.         return this.name + this.age + '运行中...'
  15. }
  16.  
  17. function Desk(name, age) {
  18.         Box.call(this, name, age);                              //对象冒充
  19. }
  20.  
  21. //通过寄生组合继承来实现继承
  22. create(Box, Desk);                                                      //这句话用来替代Desk.prototype = new Box();
  23.  
  24.  
  25. var desk = new Desk('Lee', 100);
  26. alert(desk.run());
  27. alert(desk.constructor);

回复 "寄生函数"

这儿你可以回复上面这条便签

captcha