Skip to content
Akin C edited this page Aug 16, 2017 · 16 revisions

Welcome to the Javascript-function-eval- wiki!

Javascript´s eval function takes a string as an argument. This string can contain Javascript code which will be performed:

eval
(   
    //Outputs "Hello World!"
    'alert("Hello World!")'
);

The example above is a direct call of the function eval and it could bring with its use some disadvantages. One is the ability to change code within the scope of a function in which eval is used. For avoiding that, one could use strict mode it seems or wrap eval in a nested function:

function anyFunction(code)
{
   //Nested function
   (function()
    {
        eval
        (   
            'alert(code)'
        );

    })();
}

The second issue is that the performance costs could be high. The solution for that is an indirect call:

(0,eval)(code);

It seems the listing above is just one example for an indirect eval call. The syntax could be read as follows:

Code Part Explanation
(0,eval)
                    
Follows the rule regarding to the comma operator and evaluates to a value
(code)
                    
Is the argument of the function _**eval**_(or any other function)

STILL IN WORK!

Clone this wiki locally