简体   繁体   English

Javascript使变量成为每次被引用时都会自行调用的函数

[英]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.

相关问题 可能将javascript变量的作用域限定为一个函数,并且该函数对其自身进行递归调用? - Possible to scope a javascript variable to a function and that function's recursive calls to itself? 每次我的 JavaScript 文件从其中提取代码时,如何使变量“刷新”自身? - How can I make a variable “refresh” itself every time my JavaScript file has code pulled from it? 每次运行JavaScript函数都会崩溃 - JavaScript function crashes every time it's run 我可以在JavaScript对象上创建一个属性,每次引用它时都是新的吗? - Can I create a property on a JavaScript object that's new every time it's referenced? 我试图让 JavaScript function 在特定时间间隔内每 50 毫秒运行一次,但它不起作用 - I am trying to make a JavaScript function run every 50ms for a certain time interval, but it's not working 函数调用自身时Javascript变量发生变化 - Javascript Variables Change when Function Calls Itself 一次对6个ID执行JavaScript函数(6次调用后返回?) - JavaScript function performed on 6 id's at a time (return after 6 calls ?) 如何通过函数调用找到JavaScript变量的可传递用法? - How to find a JavaScript variable's transitive usage through function calls? 每次出现时都可以让变量调用函数吗? - It is possible to make a variable call a function every time it appears? Javascript function 到每个调用 function 的文本字段 - Javascript function to every textfield that calls the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM