You can look into the vm-browserify project, which would be used in conjunction with browserify.
It works by creating s, and evaling the code in that . The code is actually pretty simple, so you could adapt the basic idea for your own purposes if you don't want to use the library itself.