Last active
August 29, 2015 14:01
-
-
Save christianbundy/bdb57f78e0ae8f0347aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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