gpt4 book ai didi

Javascript 类变量重新分配

转载 作者:行者123 更新时间:2023-12-01 00:39:37 26 4
gpt4 key购买 nike

我正在创建一个 Class 实例,我有点陷入变量重新分配,如下所示

class A {
constructor() {
this.checkBoolen = false
}

checkBoolen = false // error, asks to install @babel/plugin-proposal-class-properties to get support.

click() {
document.addEventListener('click', e => {
this.checkBoolen=true // <- a class constructor's prototype property can't be reassigned.
})
}

doSomthing() {
if(this.checkBoolen = true // <- never get there) {
console.log('do something')
setTimeout(function(){ this.checkBoolen = false}, 3000)
}
}
}

看起来我需要使用@babel/plugin-proposal-class-properties?或者将Class更改为函数?我想知道是否有办法更改 Class 内的变量,或者这是一个不好的做法?

最佳答案

有多个不匹配的括号,class关键字大写

class A {
constructor() {
this.checkBoolen = false
}

checkBoolen = false // error, asks to install @babel/plugin-proposal-class-properties to get support.

click() {
document.addEventListener('click', e => {
this.checkBoolen=true // <- a class constructor's prototype property can't be reassigned.
});
}

doSomthing() {
if(this.checkBoolen = true )// <- never get there) {
console.log('do something')
}
}

你可以像这样使用它

let obj = new A();
obj.checkBoolen=true
obj.doSomthing()

关于Javascript 类变量重新分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57816481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com