[英]Element is not exported warning for JavaScript in WebStorm
我不明白为什么我在this.errors
行this.errors
以下 JavaScript element is not exported
在 WebStorm IDE 中element is not exported
的element is not exported
。
function FooBar(userId, email) {
if (!util.isString(userId) || !userId.length) {
throw Error('FooBar(): requires a non-empty userId:String');
}
if (this instanceof FooBar) {
this.id = generateId();
this.userId = userId;
this.email = email;
this.curStep = WORKER_STEPS[0];
this.completed = false;
this.failed = false;
this.createDate = new Date();
this.errors = {};
for (let step of WORKER_STEPS) {
this.errors[step] = 0;
}
} else {
return new FooBar(userId, email);
}
}
我知道我可以得到很多反对票,因为这超出了问题的范围。
但是是时候切换到 ES6 了。
所以我会给你更好的解决方案(:
1) 开启 EcmaScript 6 支持:
2) 切换到class
es 以避免使用function
大量老式类定义
将此添加到 js 文件的顶部,它将使 nodejs 解释器提醒内存韭菜或不必要的变量定义等不错的功能。
/* jshint strict: true */
'use strict';
这是你的FooBar
:
const
_ = require('lodash');
class Entity {
hasError() {
return !_.isEmpty(this.errors);
}
addError(step, error) {
this.errors[step] = error;
}
getErrors() {
return this.errors;
}
}
class FooBar extends Entity {
constructor(userId, email, steps = []) {
if (_.isEmpty(userId) || !_.isString(userId))
throw Error('FooBar(): requires a non-empty userId:String');
this.id = generateId();
this.userId = userId;
this.email = email;
this.curStep = _.first(steps);
this.completed = false;
this.failed = false;
this.createDate = new Date();
this.errors = {};
}
}
用法:
let
fooBar = new FooBar(
'1f049de1-b592-4f17-9a7d-3f2097477b23',
'somebody@gmail.com',
WORKER_STEPS);
fooBar.addError(0, "Something happen"); // or fooBarInstance.addError("Somewhere", "Something happen");
if(fooBar.hasError()) {
console.error(fooBarInstance.getErrors());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.