将函数应用为类方法

将函数应用为类方法

本文介绍了ES6:将函数应用为类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个项目从CoffeeScript迁移到ES6(使用6to5和Browserify),并且遇到可能的限制,或者我只是不知道正确的语法。在CoffeeScript我可以这样做:

I'm migrating a project from CoffeeScript to ES6 (using 6to5 and Browserify), and am running into possibly a limitation or maybe I just don't know the proper syntax. In CoffeeScript I could do this:

 $ 到类声明后的 SomeView 原型:

class SomeView extends BaseView {
    //.. some class declaration
}
SomeView.prototype.triggerMethod = Marionette.triggerMethod;

或 Object.assign :



or with Object.assign:

class SomeView extends BaseView {
    //.. some class declaration
}

Object.assign(SomeView.prototype, {
  triggerMethod: Marionette.triggerMethod
  // ... some another methods
});


  • 您已经做了 - 添加 Marionette.triggerMethod 到 this 。但是你必须知道在这种情况下, triggerMethod 将保存在对象本身,而不是它的原型。示例:

  • What you already did - add Marionette.triggerMethod to the this. But you must be aware that in that case triggerMethod will be kept in the object itself, not in its prototype. Example:

    class SomeView extends BaseView {
        constructor() {
          this.triggerMethod =  Marionette.triggerMethod
          // ...
        }
    }
    


  • b
    $ b

    这就是你能做的。我认为第一和第二个变种是你的最佳选择,但它是一个味道的问题。

    That's all you can do. I think the first and second variants are the best choices for your case, but it's a matter of taste.

    这篇关于ES6:将函数应用为类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-16 01:46