-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger project!
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/abuse 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) |
The user interaction part should look like the content as seen below by starting "index.html" in a web browser.
Within the textarea, Javascript code could be included/written and by pressing the "COMPILE" button it will be performed! As a standard code value within the textarea an indirect eval call is implemented as should be seen in the picture above.
Note that all files should be placed in the same folder so that the functionality of the code is guaranteed.
This knowledge was gained:
-
Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman
-
Github markdown, syntax highlight of code blocks in the table cell asked by Ziav and answered by Pokechu22
-
Is there a way to get colored text in Github Flavored Markdown? asked by Roman A. Taycher and answered by Brett Zamir
-
Perfection Kills - Global eval. What are the options? by kangax