英文:
Is it possible to make my own callback for a certain static variable?
问题
以下是您要翻译的内容:
有时函数内置了callback
函数,“当发生某些事情时调用此函数”
在我的情况下,我想创建一个callback函数,当某个静态值更改后调用
例如,
class MyVariable(){
static myVal = 1;
}
const FileUploader = (props) =>{
myAlert(){
console.log("val is changed!");
}
changeValue2(){
MyVariable.myVal = 2;
myAlert();
}
changeValue3(){
MyVariable.myVal = 3;
myAlert();
}
changeValue4(){
MyVariable.myVal = 4;
myAlert();
}
}
我想在myVal
更改后调用myAlert()
。
所以目前我写了myAlert()
三次。
我想知道是否有可能像制作回调函数一样自动执行此操作当myVal更改时调用
有没有这个想法的方法?我正在使用React
英文:
Sometimes function has built-in callback
function, "This function is called when something happens"
In my case, I want to make callback function which is called after a certain static value is changed
For example,
class MyVariable(){
static myVal = 1;
}
const FileUploader = (props) =>{
myAlert(){
console.log("val is changed!");
}
changeValue2(){
MyVariable.myVal = 2;
myAlert();
}
changeValue3(){
MyVariable.myVal = 3;
myAlert();
}
changeValue4(){
MyVariable.myVal = 4;
myAlert();
}
}
I want to call myAlert()
after myVal
is changed.
so currently I write myAlert()
third times.
I wonder is it possible to do this automatically like making the callback which is called when myVal is changed
Is there any way for this idea? I am using React
答案1
得分: 2
你可以定义一个 setter 并使用一个私有字段来存储该值。
英文:
You could define a setter and use a private field to store the value.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
class MyVariable {
static #myVal = 1;
static get myVal() {
return MyVariable.#myVal;
}
static set myVal(newVal) {
if (newVal !== MyVariable.#myVal) {
console.log('changing myVal to', newVal); // or call myAlert()
MyVariable.#myVal = newVal;
}
}
}
console.log(MyVariable.myVal);
MyVariable.myVal = 2;
console.log(MyVariable.myVal);
<!-- end snippet -->
答案2
得分: 1
你可以将myVal
更改为setter,并允许注册_监听器_或_订阅者_。然后,当值发生更改时,在setter内部调用监听器。
class MyVariable {
static #subscribers = [];
static #myVal = 1;
static set myVal(newVal) {
const oldVal = this.#myVal;
if (newVal !== oldVal) {
this.#myVal = newVal;
this.#subscribers.forEach((listener) => {
listener(oldVal, newVal);
});
}
}
static get myVal() {
return this.#myVal;
}
static subscribe(listener) {
const idx = this.#subscribers.push(listener) - 1;
// return an unsubscribe function
return () => {
this.#subscribers.splice(idx, 1);
};
}
}
const unsub = MyVariable.subscribe((oldVal, newVal) => {
console.log(`val is changed! From ${oldVal} to ${newVal}`);
});
MyVariable.myVal = 2;
MyVariable.myVal = 3;
unsub();
MyVariable.myVal = 4; // no alert after unsubscribe
英文:
You can change myVal
to a setter and also allow registering of listeners or subscribers. Then, when the value is changed, call the listeners from within the setter.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
class MyVariable {
static #subscribers = [];
static #myVal = 1;
static set myVal(newVal) {
const oldVal = this.#myVal;
if (newVal !== oldVal) {
this.#myVal = newVal;
this.#subscribers.forEach((listener) => {
listener(oldVal, newVal);
});
}
}
static get myVal() {
return this.#myVal;
}
static subscribe(listener) {
const idx = this.#subscribers.push(listener) - 1;
// return an unsubscribe function
return () => {
this.#subscribers.splice(idx, 1);
};
}
}
const unsub = MyVariable.subscribe((oldVal, newVal) => {
console.log(`val is changed! From ${oldVal} to ${newVal}`);
});
MyVariable.myVal = 2;
MyVariable.myVal = 3;
unsub();
MyVariable.myVal = 4; // no alert after unsubscribe
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论