

javascript eval: direct vs indirect call
source link: https://blog.klipse.tech/javascript/2016/06/20/js-eval-secrets.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

javascript eval: direct vs indirect call
Jun 20, 2016 • Yehonathan Sharvit
Every javsacript
developer knows that eval
is evil.
But eval
is really powerful and from a theoretical perspective eval
is very interesting.
For instance, did you ever ask yourself in what context does eval
run?
In this article, we are going to show a couple of examples that relates to this question.

All the examples of this page are live and interactive code snippets:
- The code is executed in your browser
- You can modify the code and it is evaluated as you type
The interactive code snippets are powered by klipse.
Outside Scope or Inside Scope?
Usually, eval
runs inside the scope of the caller function:
xxxxxxxxxx
var context = 'outside';
(function(){
var context = 'inside';
return eval('context');
})();
"inside"
But sometimes, eval
runs outside the scope of the caller function:
xxxxxxxxxx
var context = 'outside';
(function(){
var context = 'inside';
return eval.call(null, 'context');
})();
xxxxxxxxxx
"outside"
Also in this case - outside:
xxxxxxxxxx
var context = 'outside';
(function(){
var context = 'inside';
return (1, eval)('context');
})();
xxxxxxxxxx
"outside"
But in this case - it’s inside:
xxxxxxxxxx
var context = 'outside';
(function(){
var context = 'inside';
return (eval)('context');
})();
xxxxxxxxxx
"inside"
And another one that gets outside:
xxxxxxxxxx
var context = 'outside';
(function(){
var context = 'inside';
var my_eval = eval;
return my_eval('context');
})();
xxxxxxxxxx
"outside"
It depends whether the eval
call is direct or indirect.
Furher details and explanations in this article that explains the difference between eval
direct and indirect call in EcmaScript 5
and other cool stuff.
Follow viebel to stay up-to-date with the coolest interactive articles around the world.
Discover more cool interactive articles about javascript, clojure[script], python, ruby, scheme, c++ and even brainfuck!
Give Klipse a Github star to express how much you appreciate Code Interactivity.
Subscribe to the Klipse newsletter:Feel free to email me [email protected] for getting practical tips and tricks in writing your first interactive blog post.
Recommend
-
88
Avoid speculative indirect calls in kernel Messages in this threadFrom
-
64
Create macros to restrict/unrestrict Indirect Branch Speculation Messages in this thread
-
72
-
35
Last time, I talked about the insanity of M's block and line scope . This time, I have something hopefully less insane:...
-
46
README.md
-
36
Contents: Subscribe to mymailing list and support me on Patreon . This is the second post of the series showing how one can tune the software by intros...
-
5
ARM64下Indirect Result Location摸索之前学习汇编的时候,大概了解了一些ARM64下寄存器的用途,比如x0 - x7作为函数传递使用。同时,x0也可以作为函数返回值时候的寄存器。 但是,今天在研究一些跟返回结构体相关的...
-
5
A good case for Eval in JavaScript Or: How to generate dynamic html tags inside lit-html templates? eval is sometimes mixed up with evil. We also hear sometimes that there are cases in which...
-
8
今天我们来看一个进入 statge3 的新的 JavaScript 提案:ShadowRealm api。Jav...
-
6
Difference between Direct Personal Investigation and Indirect Oral InvestigationDirect Personal Investigation and Indirect Oral Investigation are two of the many methods of collecting primary data.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK