Created
December 13, 2013 05:42
-
-
Save danmactough/7940241 to your computer and use it in GitHub Desktop.
Closure instead of with
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
[dan@Dans-MacBook-Pro code]$ node | |
> var code = 'function printer (val) { return val; }; var result = printer(a); console.log(result);' | |
undefined | |
> eval(code) | |
ReferenceError: a is not defined | |
at eval (eval at <anonymous> (repl:1:7), <anonymous>:1:62) | |
at repl:1:2 | |
at REPLServer.self.eval (repl.js:110:21) | |
at Interface.<anonymous> (repl.js:239:12) | |
at Interface.EventEmitter.emit (events.js:95:17) | |
at Interface._onLine (readline.js:202:10) | |
at Interface._line (readline.js:531:8) | |
at Interface._ttyWrite (readline.js:760:14) | |
at ReadStream.onkeypress (readline.js:99:10) | |
at ReadStream.EventEmitter.emit (events.js:98:17) | |
> (function () { | |
... var a = 1; | |
... eval(code); | |
... })(); | |
1 | |
undefined | |
> (function (ctx) { | |
... for (var k in ctx) { | |
..... eval('var ' + k + ' =ctx[k]') | |
..... } | |
... eval(code); | |
... })({ a: 1 }); | |
1 | |
undefined | |
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment