20
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?