Created
July 22, 2014 08:58
-
-
Save samarpanda/e39e03aad9fda3d40aa5 to your computer and use it in GitHub Desktop.
Difference between using eval and new Function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * test1(); | |
| * Output: 22 | |
| */ | |
| function test1(){ | |
| var a = 11; | |
| eval('(a = 22)'); | |
| alert(a); | |
| } | |
| /** | |
| * test2(); | |
| * Output: 11 | |
| */ | |
| function test2(){ | |
| var a = 11; | |
| new Function('return (a=22);')(); | |
| alert(a); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment