jQuery 如何使用jquery创建动态变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19976886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:52:07  来源:igfitidea点击:

How to create dynamic variables using jquery?

javascriptjquery

提问by Deepak A

I want some jquery variables to be created dynamically. In my code I am having a loop, and with the loop values I want to create some variables. Here is my sample code.

我想要动态创建一些 jquery 变量。在我的代码中,我有一个循环,并且我想使用循环值创建一些变量。这是我的示例代码。

array=["student","parent","employee"]

$.each(user_types, function( index, value ){
  var value+"_type" // this is the type of variable i want to build.
})

I have found about evalfunction. That code goes like this.

我发现了eval函数。那个代码是这样的。

var type = "type"
eval("var pre_"+type+"= 'The value of dynamic variable, val';");

alert(pre_type) // this gives 'The value of dynamic variable, val' in alert box.

Is there any alternate ways as I have read the eval function is not prefered while coding .jsfiles.

是否有其他替代方法,因为我已经阅读了 eval 函数在编码.js文件时不是首选。

回答by George

Any time you find yourself using a variable in the name of a variable, you probably want to use an object literal. Create the object with curly braces {}, and then set the object property key using square bracket notation:

每当您发现自己在变量名中使用变量时,您可能想要使用对象字面量。使用花括号创建对象{},然后使用方括号表示法设置对象属性键:

var user_types = ["student","parent","employee"];
var types = {};

$.each(user_types, function( index, value ){
  types[value] = 'The value of dynamic variable, val';
});

JSFiddle

JSFiddle

Note: You haven't tagged it, but I assume because you've used each()that you are using jQuery, please correct me if I'm wrong.

注意:您没有标记它,但我假设因为您已经使用each()过您正在使用 jQuery,如果我错了,请纠正我。

回答by Tibos

First of all i must say that i can't think of any reason why you want to do this.

首先,我必须说,我想不出你为什么要这样做的任何理由。

If you really need to have those variables, in global scope, you can do the following:

如果您确实需要在全局范围内拥有这些变量,则可以执行以下操作:

var array=["student","parent","employee"]

array.forEach(function(value){
  window[value+"_type"] = 'My value ' + value;
});

console.log(student_type);
console.log(parent_type);
console.log(employee_type);

If you don't want the variables in global scope, i'm afraid i don't know an elegant solution.

如果您不想在全局范围内使用变量,恐怕我不知道一个优雅的解决方案。

I used array.forEachinstead of your jQuery loop because the problem is not related to jQuery at all and because i don't think you said enough of your logic to make a coherent example.

我使用array.forEach了你的 jQuery 循环而不是你的 jQuery 循环,因为这个问题根本与 jQuery 无关,而且因为我认为你没有足够的逻辑来制作一个连贯的例子。

EDIT: I should make it clear that while the 'variables' created behave mostly like other variables in global scope, they are NOT variables. Here is how they differ:

编辑:我应该明确指出,虽然创建的“变量”的行为与全局范围内的其他变量大致相同,但它们不是 variables。以下是它们的不同之处:

// Difference 1: hoisting
console.log(x); // undefined
console.log(y); // ReferenceError: y is not defined
var x = 5;
window[y] = 5;
console.log(x); // 5
console.log(y); // 5
// Difference 2: [[Configurable]] 
delete x;
delete y;
console.log(x); // 5
console.log(y); // ReferenceError: y is not defined