LoginSignup
19
16

More than 5 years have passed since last update.

文字列を式として評価する方法4種

Posted at

他にもありそう。

// type 1
eval('var a=10; alert(a + 7)');

// type 2
new Function('var a=10; alert(a + 7)')();

// type 3
var f = function(){}
f.constructor('var a=10; alert(a + 7)')();

// type 4
location='javascript:var a=10; alert(a + 7)';

new Functionは引数が取れたりして面白い。


var str =  'return x + y';
var f = new Function('x', 'y', str);

f(10, 7); //->17
19
16
4

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
19
16