使用时如何检测变量? [英] How to detect the variable when used?
本文介绍了使用时如何检测变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在每次调用变量时调用一个函数?下面的代码是可能的吗?我打算每次使用"num"时都调用一个函数.
how to call a function every call of a variable? is the code below is possible? i'm planning to call a function every time "num" is used..
if (num == 100 && num== 200 && num==300) {
console.log('it works!');
}
推荐答案
valueOf
滥用是一种选择,但还有许多其他选择.
valueOf
abuse is one option, but there are many others.
const num = {
value: 0,
valueOf: function() {
this.value += 100;
return this.value;
}
}
if (num == 100 && num== 200 && num==300) {
console.log('it works!');
}
这篇关于使用时如何检测变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文