Skip to content

Instantly share code, notes, and snippets.

@christianbundy
Last active August 29, 2015 14:01
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 christianbundy/bdb57f78e0ae8f0347aa to your computer and use it in GitHub Desktop.
Save christianbundy/bdb57f78e0ae8f0347aa to your computer and use it in GitHub Desktop.
var strict = function (fn) {
'use strict';
var args;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARG_NAMES = /([^\s,]+)/g;
var fnStr = fn.toString().replace(STRIP_COMMENTS, '');
args = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARG_NAMES);
if (args === null) {
args = [];
}
var getGlobals = function (obj, args) {
var globals = [];
args.forEach(function (arg) {
globals.push(obj[arg]);
});
fn.apply(obj, globals);
};
if (typeof window !== 'undefined') {
getGlobals(window, args);
} else if (typeof global !== 'undefined') {
getGlobals(global, args);
} else {
throw "Can't find window/global object. Are you doing something weird?";
}
};
strict(function ($) {
console.log(typeof $); // read-only global
$ = null;
console.log(typeof $);
console.log(typeof this.$); // read/write global
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment