【问题标题】:this._super() vs this.super()this._super() 与 this.super()
【发布时间】:2014-08-29 12:58:49
【问题描述】:

我想知道为什么像 EmberJS 这样的框架使用_super 方法来调用扩展对象上的覆盖方法,而不是(在我看来)更合乎逻辑的this.super

这是否有特定原因,例如 super 是 javascript 中的保留关键字?因此,您不能将变量或命名函数分配给名称super。分配给this.super 的函数是匿名函数吧?

【问题讨论】:

  • 只是编码风格。
  • super 在所有以前的 ECMAScript 版本中都是 Future Reserved Word。第 5 版确实允许用户使用保留字作为(未引用的)属性。但是,例如 IE8 可能会引发语法错误,因为它基于第 3 版。

标签: javascript ember.js super


【解决方案1】:

_ 表示私有成员。您只能从类内部访问_super,而不能从外部访问。

【讨论】:

    【解决方案2】:

    _super 可用于区分可能已设置的其他功能。这可能是为了展示它的特殊性,或者与其他变量不同。或者它可能是显示它是私人的。

    见:

    When do you make an underscore in front of an instance variable?

    How does an underscore in front of a variable in a cocoa objective-c class work?

    Why do we use _ in variable names?

    编辑:就像 Barmar 所说的(除了少得多的话)它是一种编码风格,没有别的。

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 2014-12-02
      • 1970-01-01
      • 2017-04-22
      • 2016-05-17
      • 2019-08-30
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多