admin管理员组文章数量:1245688
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:
class SomeView extends BaseView
triggerMethod: Marionette.triggerMethod
How do I express this in ES6 classes? I tried a couple of things, but it throws Unexpected token
errors no matter what I try. This for example:
let { triggerMethod } = Marionette;
class SomeView extends BaseView {
triggerMethod, // doesn't work
triggerMethod: Marionette.triggerMethod // doesn't work
}
Now I can achieve this by setting it in the constructor (this.triggerMethod = Marionette.triggerMethod
), but it feels a bit ugly to me (just a preference in coding style I guess). Any help would be appreciated.
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:
class SomeView extends BaseView
triggerMethod: Marionette.triggerMethod
How do I express this in ES6 classes? I tried a couple of things, but it throws Unexpected token
errors no matter what I try. This for example:
let { triggerMethod } = Marionette;
class SomeView extends BaseView {
triggerMethod, // doesn't work
triggerMethod: Marionette.triggerMethod // doesn't work
}
Now I can achieve this by setting it in the constructor (this.triggerMethod = Marionette.triggerMethod
), but it feels a bit ugly to me (just a preference in coding style I guess). Any help would be appreciated.
2 Answers
Reset to default 15You can't declare properties in ES6 classes, only methods and static methods (see here for syntax of class declaration, pay attention to ClassElement). So any of the following examples will be wrong:
class A {
method1: B.someMethod // wrong!
method2: function() {} // wrong!
method3: () => {} // wrong!
}
You have several variants to handle your problem:
Use getter:
class SomeView extends BaseView { get triggerMethod() { return Marionette.triggerMethod } }
Call
Marionette.triggerMethod
fromtriggerMethod
ofSomeView
class:class SomeView extends BaseView { triggerMethod() { Marionette.triggerMethod.apply(this, arguments); } }
Add
triggerMethod
to the prototype ofSomeView
after class declaration:class SomeView extends BaseView { //.. some class declaration } SomeView.prototype.triggerMethod = Marionette.triggerMethod;
or with
Object.assign
:class SomeView extends BaseView { //.. some class declaration } Object.assign(SomeView.prototype, { triggerMethod: Marionette.triggerMethod // ... some another methods });
What you already did - add
Marionette.triggerMethod
to thethis
. But you must be aware that in that casetriggerMethod
will be kept in the object itself, not in its prototype. Example:class SomeView extends BaseView { constructor() { this.triggerMethod = Marionette.triggerMethod // ... } }
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.
If I got this right you can define class methods this way:
class SomeView extends BaseView {
triggerMethod(){
Marionette.triggerMethod;
}
}
Pretty much the same as in coffeeScript, but some braces are required. I haven't tried this, but see http://wiki.ecmascript/doku.php?id=harmony:classes
本文标签: javascriptES6 Applying function as class methodStack Overflow
版权声明:本文标题:javascript - ES6: Applying function as class method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740232125a2245794.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论