Skip to content

Instantly share code, notes, and snippets.

@jthomas
Last active January 31, 2016 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jthomas/69341518f4a6940e8874 to your computer and use it in GitHub Desktop.
Save jthomas/69341518f4a6940e8874 to your computer and use it in GitHub Desktop.
Accessing variables from closure
var vm = require('vm');
var Debug = vm.runInDebugContext('Debug');
Debug.setListener(function (event, exec_state) {
if (event !== Debug.DebugEvent.Break) return;
try {
console.log(exec_state.frame(0).evaluate('inner'))
} catch (err) {
console.log(err)
}
});
function fails() {
var inner = 'inner';
var breakpoint = function () {
debugger;
}
breakpoint();
}
function works() {
var inner = 'inner';
var breakpoint = function () {
var ref = inner;
debugger;
}
breakpoint();
}
works();
fails();
@jthomas
Copy link
Author

jthomas commented Jan 31, 2016

$ node test.js
StringMirror { type_: 'string', value_: 'inner', handle_: 8 }
[ReferenceError: inner is not defined]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment