简体   繁体   English

Javascript Newb:如何将var实例化为“ blah.1”和“ blah.2”?

[英]Javascript Newb : How do I instantiate a var as “blah.1” and “blah.2”?

I currently have a block like this defining some vars 我目前有一个这样的块定义一些变量

var slider_1  = document.querySelector('#slider_1');
var slider_2  = document.querySelector('#slider_2');
...

And func's that take ID's like this: 带有ID的func是这样的:

function updateFromInput(id){               
    if(id==1){
        var x = input_1.value*1;
        x = Math.round((x*ratio)-offset);     
        slider_1.x.baseVal.value =  x/scale;
    }else if(id==2){
        var x = input_2.value*1;
        x = Math.round((x*ratio)-offset); 
        slider_2.x.baseVal.value =  x/scale;
    }
};

I am trying to refactor a bit. 我正在尝试重构。
I'm thinking that if I could, instead, instantiate my vars with dots rather than underscores like 我在想,如果可以的话,可以用点而不是下划线来实例化变量

var slider.1  = document.querySelector('#slider_1');
var slider.2  = document.querySelector('#slider_2');

then I'd be able to better utilize the ID already getting passed into my func's and eliminate tons of duplication. 那么我将能够更好地利用已经传递到函数中的ID并消除大量重复。
I was hoping to simplify my funcs with something like a single call for slider.id.x.baseVal.value = x/scale; 我希望通过单次调用slider.id.x.baseVal.value = x/scale;来简化功能slider.id.x.baseVal.value = x/scale; rather than having to have that code in each of the IF/ELSE conditions. 而不是必须在每个 IF / ELSE条件中都使用该代码。

When I try that though, I get an error saying " Uncaught SyntaxError: Unexpected number ". 但是,当我尝试这样做时,出现错误消息“ Uncaught SyntaxError:Unexpected number ”。
How should this be done? 应该怎么做?

You can't use a plain numeric key in an object. 您不能在对象中使用纯数字键。

You can do this, though: 您可以执行以下操作:

var slider = {};  // or = [], if array syntax is more appropriate
slider[1] = ...
slider[2] = ...

Furthermore, the syntax you suggested isn't allowed if the key is actually a variable rather than a literal token. 此外,如果键实际上是变量而不是文字标记,则不允许使用您建议的语法。

In your example slider.id actually refers to the object with literal key id , not whatever value the variable id happens to have. 在您的示例中, slider.id实际上是指具有文字 id的对象,而不是变量 id碰巧具有的任何值。

You have to put the variable inside square brackets, ie slider[id] , so your function would be written thus: 您必须将变量放在方括号内,即slider[id] ,因此您的函数应这样编写:

function updateFromInput(id){               
    var x = +input[id].value;
    x = Math.round((x*ratio)-offset);     
    slider[id].x.baseVal.value =  x/scale;
};

You can't. 你不能 The . . is an invalid character for a variable identifier. 是变量标识符的无效字符。

You can use it in object properties though. 不过,您可以在对象属性中使用它。

var sliders = {
    "slider.1": document.querySelector('#slider_1'),
    "slider.2": document.querySelector('#slider_2')
};

Then use the square bracket version of the member operator to access the property. 然后使用成员运算符的方括号版本访问属性。

alert( sliders["slider.1"].id );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript命名空间TypeError:blah不是函数 - javascript namespacing TypeError: blah is not a function javascript国际化和列表(例如[foo,bar,blah]改为“ foo,bar和blah”) - javascript internationalization and lists (eg [foo,bar,blah] to “foo, bar and blah”) 如何在使用href = javascript时保持url显示:blah() - how to keep the url from displaying when using a href=javascript:blah() 我可以仅在浏览器窗口中的图像上调用Javascript小书签吗? 例如:blah.com/blah.jpg? - Can I call a Javascript bookmarklet over just an image in browser window. e.g.:blah.com/blah.jpg? 在 Angular 中是否有等价于“blah”+ 变量 +“blah”? - Is there an equivalent of “blah” + variable + “blah” in Angular? 如何在 Chrome Devtools JS 控制台中从“http://bar.org/some-esm-module.js”中“导入 blah”? - How do I `import blah from 'http://bar.org/some-esm-module.js'` in the Chrome Devtools JS console? / blah / blah / add处的NoReverseMatch,尽管我认为我的模板if / else / endif将解决该错误 - NoReverseMatch at /blah/blah/add though I thought my template if/else/endif would solve the error Javascript'未捕获的TypeError:无法读取未定义'的属性'blah' - Javascript 'Uncaught TypeError: Cannot read property 'blah' of undefined ' 对象未设置为实例…等等 - Object not set to instance…blah 类型错误:找不到 function 等等 - TypeError: Cannot find function blah in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM