[英]Class properties are giving no-undef
I'm trying to make a non-component class in ReactJS.我正在尝试在 ReactJS 中制作非组件 class。 Most tutorials I've followed on JavaScript show that when you're making a class, you can create properties simply by specifying them with
this.[name]
.我在 JavaScript 上关注的大多数教程都表明,当您制作 class 时,您只需使用
this.[name]
指定它们即可创建属性。
Something really doesn't like this, and it throws me a no-undef
error for my properties.有些东西真的不喜欢这样,它给我的属性抛出了一个
no-undef
错误。
Here is the code in question:这是有问题的代码:
class MicroBitConnection {
constructor(device) {
this.device = device;
this.connectDAP();
}
connectDAP() {
this.transport = new DAPjs.WebUSB(device);
this.dap = new DAPjs.DAPLink(transport);
}
}
Right now I don't even instantiate the class anywhere, it gives me this error right off the bat:现在我什至没有在任何地方实例化 class,它立即给了我这个错误:
Line 11:43: 'device' is not defined no-undef
Line 12:44: 'transport' is not defined no-undef
It doesn't seem like a linting problem, because even with this warning off using /* eslint no-undef: 0 */ // --> OFF
at the top of the file, actually running the code with the class instantiated somewhere gives the same error:这似乎不是一个 linting 问题,因为即使在文件顶部使用
/* eslint no-undef: 0 */ // --> OFF
此警告,实际运行在某处实例化 class 的代码也会给出同样的错误:
Error: ReferenceError: device is not defined
In your connectDAP
method you are referencing undefined variables device
and transport
.在您的
connectDAP
方法中,您引用了未定义的变量device
和transport
。 You probably want to use the properties defined on the instance this
:您可能希望使用在实例
this
上定义的属性:
class MicroBitConnection {
constructor(device) {
this.device = device;
this.connectDAP();
}
connectDAP() {
this.transport = new DAPjs.WebUSB(this.device);
this.dap = new DAPjs.DAPLink(this.transport);
}
}
Please also note that this
is not automatically bound to the instance, eg when using an instance method as an event handler.另请注意,
this
不会自动绑定到实例,例如当使用实例方法作为事件处理程序时。 To make sure it's bound to the instance in all circumstances, call Function.prototype.bind
in the constructor:要确保它在所有情况下都绑定到实例,请在构造函数中调用
Function.prototype.bind
:
this.connectDAP = this.connectDAP.bind(this);
If you want to omit this
you need to locally declare the variables, which can easily be done using destructuring:如果你想省略
this
,你需要在本地声明变量,这可以很容易地使用解构来完成:
class MicroBitConnection {
constructor(device) {
this.device = device;
this.connectDAP();
}
connectDAP() {
const { device } = this;
this.transport = new DAPjs.WebUSB(device);
const { transport } = this;
this.dap = new DAPjs.DAPLink(transport);
}
}
A third alternative (not recommended) is using with
statement:第三种选择(不推荐)是使用
with
语句:
class MicroBitConnection {
constructor(device) {
this.device = device;
this.transport = null;
this.dap = null;
this.connectDAP();
}
connectDAP() {
with (this) {
transport = new DAPjs.WebUSB(device);
dap = new DAPjs.DAPLink(transport);
}
}
}
I'm not sure this last version does what you expect, just listing it for completeness' sake and to make you aware of the existence of the with
statement.我不确定最后一个版本是否符合您的期望,只是为了完整起见将其列出并让您知道
with
语句的存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.