学习《你不知道的JavaScript》遇到的问题:Uncaught SyntaxError: ‘super‘ keyword unexpected here

0 前言:

1. 不同于传统面向类的语言中父类和子类、子类和实例之间的关系(复制操作),在JavaScript中并没有复制,实现类这一设计模式依靠的是 [[Prototype]] ,故父类和子类、子类和实例之间只有委托关联。

1 正文:

1. 由于这一委托关联,在es6之前想要实现类,往往利用的是  [[Prototype]];

// 传统JavaScript的类应用(prototype)
// 父类
function Widget(width, height) {
  this.width = width || 50;
  this.height = height || 50;
  this.$elem = null;
}
Widget.prototype.render = function ($where) {
  if (this.$elem) { // 
    this.$elem.css({
      width: this.width + "px",
      height: this.height + "px"
    }).appendTo($where);
  }
};
// 子类
function Button(width, height, label) {
  // 调用“super”构造函数
  Widget.call(this, width, height);
  this.label = label || "Default";
  this.$elem = $("<button>").text(this.label);
}
// 让 Button“继承”Widget
Button.prototype = Object.create(Widget.prototype);
// 重写 render(..)
Button.prototype.render = function ($where) {
  //“super”调用
  Widget.prototype.render.call(this, $where);
  this.$elem.click(this.onClick.bind(this));
};
Button.prototype.onClick = function (evt) {
  console.log("Button '" + this.label + "' clicked!");
};
$(document).ready(function () {
  var $body = $(document.body);
  var btn1 = new Button(125, 30, "Hello");
  var btn2 = new Button(150, 40, "World");
  btn1.render($body);
  btn2.render($body);
});

2. ES6 之后,新出现了 Class 这一类的概念,于此紧密相关的是 super 这一关键字;其最大的作用是:用于访问对象字面量或类的原型([[Prototype]])上的属性,或调用父类的构造函数。

根据原书给出的关于利用 Class 和 super 实现 JavaScript 中类的代码如下:

class Widget {
  constructor(width, height) {
    this.width = width || 50;
    this.height = height || 50;
    this.$elem = null;
  }
  render($where) {
    if (this.$elem) {
      this.$elem.css({
        width: this.width + "px",
        height: this.height + "px"
      }).appendTo($where);
    }
  }
}
class Button extends Widget {
  constructor(width, height, label) {
    super(width, height);
    this.label = label || "Default";
    this.$elem = $("<button>").text(this.label);
  }
  render($where) {
    super($where);
    this.$elem.click(this.onClick.bind(this));
  }
  onClick(evt) {
    console.log("Button '" + this.label + "' clicked!");
  }
}
$(document).ready(function () {
  var $body = $(document.body);
  var btn1 = new Button(125, 30, "Hello");
  var btn2 = new Button(150, 40, "World");
  btn1.render($body);
  btn2.render($body);
});

3. 问题:

执行上述代码,浏览器会产生错误:

 经排查发现,这是对super的使用上出现问题。

4. 解决

具体解决方法和思路如下

 render($where) {
    // super($where); //原书描述,经测试出错。应改为super.render($where);此处不应该是对父类中的变量进行引用,而是rander()这一方法本身。
    // 因为根据 MDN 在对象字面量中使用super.prop:在子类中调用父类的方法,这是在Object.setPrototyprOf()的帮助下实现的;
    // 故将 子类的原型设置为父类,这样super就能在父类上找到父类的方法。
    super.render($where)
    this.$elem.click(this.onClick.bind(this));
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值