Skip to content

Instantly share code, notes, and snippets.

@danmactough
Last active Aug 29, 2015
Embed
What would you like to do?
var vm = require('vm');
var boss = 'Mr. Trump'
, condition = 'bald';
var script = [
'boss = name;',
'condition = type;',
'result = boss + " is " + condition;'
].join('\n');
var ctx = {
name: 'Dan',
type: 'awesome'
};
var evaled = vm.runInContext(script, vm.createContext(ctx));
console.log(evaled);
// Dan is awesome
ctx = {
name: boss,
type: condition
};
var evaled = vm.runInContext(script, vm.createContext(ctx));
console.log(evaled);
// Mr. Trump is bald
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment