0
0

【jQuery】【JavaScript】evalを活用して文字列を変数にする※globalthis でもできる

Last updated at Posted at 2024-08-06

eval


var aaa = 111;
var bbb = 222;
var ccc = 333;
var ddd = 444;

const results = ['aaa', 'bbb', 'ccc'];

$.each(results, function(k, v) {
    console.log(eval(v));
});

 console.log(eval('d'+'d'+'d'));

globalThis


var aaa = 111;
var bbb = 222;
var ccc = 333;
var ddd = 444;

const results = ['aaa', 'bbb', 'ccc'];

$.each(results, function(k, v) {
    console.log(globalThis[v]);
});

console.log(globalThis['d'+'d'+'d']);

結果

111
222
333
444
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0