[英]Javascript make variable a function that calls itself every time it's referenced
Hi I would like to know if it is possible to make a variable behave like this: 嗨,我想知道是否可以使变量的行为如下:
var someFlag = true;
var someWidth = (()=>(someFlag) ? 16 : 12)();
console.log(someWidth) // 16
someFlag = false;
console.log(someWidth) // would be nice if it was 12
// right now it still prints 16
Is something like this possible? 这样的事情可能吗? Also I know that someWidth could be a function, the idea is I have a lot of code I would need to refactor so I don't want to do someWidth(); 我也知道someWidth可能是一个函数,这个想法是我有很多代码需要重构,所以我不想做someWidth();
Great way to describe object property is here 描述对象属性的好方法在这里
var someFlag = true; Object.defineProperty(window, 'someWidth', { get:()=>someFlag ? 16 : 12 }) console.log(someWidth) // 16 someFlag = false; console.log(someWidth) // 12
I dont think its good idea to have such global variables.You can try to create your own object maybe? 我不认为拥有这样的全局变量是个好主意。您可以尝试创建自己的对象吗?
Short answer: Yes, but it's not recommended. 简短答案:是,但不建议这样做。
If a name does not belong to a local variable, then it is looked for as the name of a property of the global object - which on the browser is window
. 如果名称不属于局部变量,则将其视为全局对象的属性名称-在浏览器中为window
。 It is possible to use defineProperty
on the window
object to define a property with a getter function. 可以在window
对象上使用defineProperty
来使用getter函数定义属性。
Object.defineProperty(window, 'test', {
get: function () { return 3; },
});
var x = test; // x = 3
However this is absolutely not recommended, because there is only one global context. 但是,绝对不建议这样做,因为只有一个全局上下文。 In the event that multiple separate JS modules define the same global variable, the definitions will clash and you will get crashes or some odd behavior. 如果多个单独的JS模块定义了相同的全局变量,则这些定义会发生冲突,并且会导致崩溃或某些奇怪的行为。
Another thing you can do that's not recommended, is to use a with
block, which would look like this: 您不建议做的另一件事是使用with
块,它看起来像这样:
var o = {};
Object.defineProperty(o, 'test', {
get: function () { return 3; },
});
with (o) {
var x = test; // x = 3
}
The with
keyword takes one object as a parameter. with
关键字将一个对象作为参数。 Names inside the block are looked up first as properties of that object, then as local variables, then as properties of the global object. 首先将查找块中的名称作为该对象的属性, 然后作为局部变量, 然后作为全局对象的属性。 This is not recommended either, because it requires that all names to be looked for first as properties of the object, which slows down code. 也不建议这样做,因为它要求首先查找所有名称作为对象的属性,这会降低代码的速度。
with (o) {
var test = 4;
var x = test; // x = 3
}
Also, Javascript has lexical scoping, not dynamic scoping, so you can't just wrap the top-level entry point in a with
block, the whole program would have to be written inside a with
block. 另外,JavaScript具有词法作用域,而不是动态作用域,因此您不能仅将顶级入口点包装在with
块中,整个程序必须写在with
块内。
var f = function () {
return test;
};
with (o) {
var x = f(); // reference error: test is not defined
}
I'm not sure if this would be a solution depending on what kind of refactoring you're looking at, but how about creating your own object with a modified getter/setter? 我不确定这是否是一种解决方案,具体取决于您正在查看的是哪种重构,但是如何使用修改的getter / setter创建自己的对象呢?
var VariableWidth = { flag: true, get value() { return this.flag ? 16 : 12; }, set setFlag(p_bool) { this.flag = p_bool; } } console.log(VariableWidth.value) // 16 VariableWidth.setFlag = false; console.log(VariableWidth.value) // would be nice if it was 12 // right now it still prints 16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.