Skip to content

Instantly share code, notes, and snippets.

@leecade
Forked from 140bytes/LICENSE.txt
Created October 8, 2011 08:16
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 leecade/1272010 to your computer and use it in GitHub Desktop.
Save leecade/1272010 to your computer and use it in GitHub Desktop.
globalEval
function (
a, //string
b //placeholder
){
b = window,
'' + a === a && /\S/.test(a) && //check the arguments(not only NUL)
(b.execScript || //IE || chrome
function() {b["eval"].call(b, a) //fix the eval's scope
})(a)
}
function (a,b){b=window,""+a===a&&/\S/.test(a)&&(b.execScript||function(){b.eval.call(b,a)})(a)}
{
"name": "globalEval",
"description": "globalEval",
"keywords": [
"globalEval",
"javascript",
"斯人",
"leecade"
]
}
@tsaniel
Copy link

tsaniel commented Oct 8, 2011

I don't quite understand what is the use of /\S/.test...
By the way, you could use ''+a===a instead of typeof a === "string" to check if a is a string.
Also, if window is not the only way to represent the global object, use this or self instead.

Those skills can be learnt from https://github.com/jed/140bytes/wiki/Byte-saving-techniques

@leecade
Copy link
Author

leecade commented Oct 8, 2011

thanks tsaniel, /\S/.test used to make sure that not only NUL

@tsaniel
Copy link

tsaniel commented Oct 8, 2011

I think that is superfluous, `''+a===a`` is enough to prevent NULL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment