0

My Questions regarding eval() function in JavaScript

  1. To which object does eval() belong to ?
  2. We know that all java script code gets a execution context . What execution context does eval() get ?Is it the global execution context ?
  3. Why is eval() called an evil? Is there any case where eval() function can be a life saver ?
Inquisitive
  • 7,476
  • 14
  • 50
  • 61

1 Answers1

0
  1. When you omit an object you actually call it on the window object. So its window.eval, but since its the default its the same as a global scope.
  2. Eval scope is tricky. Basicly global scope.
  3. Eval is very evil, you allow the user to execute arbitrary code. This is a very scary thing. Especially when it has user input you do not want to use eval. Any malicious user could really give you a bad time.
TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • @Niklas B . Why do you say that executing malicious code in the browser context is not dangerous ? Isn't the client machine at risk ? – Inquisitive Jun 22 '12 at 12:57
  • I just found a useful case for eval() inside http://24ways.org/2005/dont-be-eval under the head **What’s it good for?** – Inquisitive Jun 22 '12 at 13:16
  • @Niklas B By "Javascript environment is user-controlled anyway" ,what did u actually mean ? Can you shed some light on it using examples if possible ? – Inquisitive Jun 22 '12 at 13:19