Skip to content

Instantly share code, notes, and snippets.

@hongymagic
Forked from bjouhier/hackGlobal.js
Created June 3, 2012 06:22
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 hongymagic/2862221 to your computer and use it in GitHub Desktop.
Save hongymagic/2862221 to your computer and use it in GitHub Desktop.
Masking eval, extracting FunctionBody – not really sure where it would be used.
// Extract FunctionBody as eval expects FunctionBody
Function.prototype.getBody = function() {
var m = this.toString().match(/\{([\s\S]*)\}/m)[1];
return m.replace(/^\s*\/\/.*$/mg,'');
};
var GLOBAL = "GLOBAL";
function foo () {
console.log(GLOBAL, this);
}
function bar () {
setTimeout(function () {
console.log(GLOBAL, this);
}, 0);
}
function hack () {
console.log(window);
}
foo(); // "GLOBAL"
bar(); // "GLOBAL"
// This function does not restrict access to `window`
function mask (func) {
"use strict";
var GLOBAL = 5;
eval(func.getBody());
}
mask(foo); // 5
mask(bar); // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment