我试图在ecmascript 6中定义一个类。
以下是代码:
class Machine {
constructor (){
this._context = null;
}
get context (){
return this._context;
}
set context (context){
this._context = context;
}
}
但是我总是在Webstorm的setter中得到同样的错误: " Set accessor方法的类型与get访问器类型"
不兼容我不明白为什么会收到此错误。我完全按照这里解释的那样做了:http://es6-features.org/#GetterSetter
编辑:似乎问题只出现,因为我在角度工厂中定义了我的课程。
所以我的问题是如何在角度工厂中正确定义一个类?
也许我不应该这样做。
编辑2 :这是我的角度工厂:
angular.module('frontEndApp')
.factory('Machine', function () {
class Machine {
constructor (){
this._context = null;
}
get context (){
return this._context;
}
set context (context){
this._context = context;
}
}
return Machine;
}
答案 0 :(得分:3)
您的ES6(ES2015)代码是否正确。听起来像WebStorm中关于新语法的一个错误(虽然“类型”这个词很令人惊讶,因为JavaScript是松散类型的;你可能想要检查你没有把它设置为TypeScript或类似的东西。)
答案 1 :(得分:2)
我猜WebStorm无法确定属性this._context
的类型
你可能想要注释你的代码以帮助WebStorm(我有同样的问题):
class Machine {
constructor (){
this._context = null;
}
/**
* Proxy method for getting context.
*
* @return {ContextInterface}
*/
get context (){
return this._context;
}
/**
* Sets the appropriate context.
*
* @param {ContextInterface} context
*/
set context (context){
this._context = context;
}
}
答案 2 :(得分:0)
Webstorm设置存在问题。你有一个短片或其他东西跑吗?是自动检测语言还是出于某种原因不能选择JS?
代码很好。