【问题标题】:calling overriden methods base in ES6在 ES6 中调用重写方法基
【发布时间】:2026-01-04 08:25:01
【问题描述】:

给我以下代码:

import Authenticator from 'simple-auth-torii/authenticators/torii';

export default Authenticator.extend({
  restore: function(data) {

  },
  authenticate: function(provider, options) {

  },
  invalidate: function(data) {

  }
});

如何从authenticate 方法内部调用基类authenticate 方法?

【问题讨论】:

标签: javascript ecmascript-6 ember-simple-auth


【解决方案1】:

在常见的 ES5 情况下,不能使用 Ember.Object.extend 方法和 this._super,也不能使用 ES6 类继承,必须使用标准的 callapply 方法来实现。因此,要调用父类的authenticate 方法,您必须将其添加到您的子方法中:

Authenticator.prototype.authenticate.call(this /* some args here */);

【讨论】:

    【解决方案2】:

    这实际上是一个 Ember.js 问题,并不特定于 ES6。打电话给例如

    this._super(provider, options)
    

    【讨论】: