1.将一个字符串转换为变量名
[javascript] view plain copy
- function string_to_name(string){
- let _name = ‘var new_name=‘ + string;
- eval(_name);
- return _name;
- }
2.将一个字符串转换为变量并赋值
[javascript] view plain copy
- var str=‘string‘;
- var _script=‘var ‘+ string +‘ =123;‘;
- eval(_script);
- console.log(typeof string);//number
- console.log(string);//123
- string=456;
- console.log(string);//456
时间: 2024-11-03 22:04:45