globalEval
-
-
Save leecade/1272010 to your computer and use it in GitHub Desktop.
globalEval
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
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) | |
} |
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
function (a,b){b=window,""+a===a&&/\S/.test(a)&&(b.execScript||function(){b.eval.call(b,a)})(a)} |
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
{ | |
"name": "globalEval", | |
"description": "globalEval", | |
"keywords": [ | |
"globalEval", | |
"javascript", | |
"斯人", | |
"leecade" | |
] | |
} |
thanks tsaniel, /\S/.test used to make sure that not only NUL
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
I don't quite understand what is the use of
/\S/.test
...By the way, you could use
''+a===a
instead oftypeof a === "string"
to check if a is a string.Also, if
window
is not the only way to represent the global object, usethis
orself
instead.Those skills can be learnt from https://github.com/jed/140bytes/wiki/Byte-saving-techniques