in the face of evil features such as eval or with, the YUI Compressor takes a defensive approach by not obfuscating any of the scopes containing the evil statementjavascript
传送门:官方文档
翻译成中文就是说:eval和with语句所处的做用域,YUI Compressor是不进行压缩混淆单词的。html
1)源代码java
function testCompressor(){ // 测试YUI Compressor var person = { 'name':'wall', 'jser':true }; console.log("my name is "+ person.name); }
2)压缩后从新格式化的代码git
function testCompressor() { var a = { name: "wall", jser: true }; console.log("my name is " + a.name) };
1)源代码github
function testCompressor(){ // 测试YUI Compressor var person = { 'name':'wall', 'jser':true }; console.log("my name is "+ person.name); eval("console.log(\"I am Jser\");"); }
2)压缩后从新格式化的代码app
function testCompressor() { var person = { name: "wall", jser: true }; console.log("my name is " + person.name); eval('console.log("I am Jser");') }
结论:只要eval存在的做用域,代码都没法作混淆操做。测试
既然没法改变这个规则,那就要尽可能去避免代码中直接出现eval。能够讲eval封装起来调用。
1)源代码ui
function testCompressor(){ // 测试YUI Compressor var person = { 'name':'wall', 'jser':true }; console.log("my name is "+ person.name); myEval("console.log(\"I am Jser\");"); } function myEval(data){ return eval(data); }
2)压缩后从新格式化的代码翻译
function testCompressor() { var a = { name: "wall", jser: true }; console.log("my name is " + a.name); myEval('console.log("I am Jser");') } function myEval(data) { return eval(data) }
3)另外一种解决方式: 改成window["eval"]进行调用code
function testCompressor(){ // 测试YUI Compressor var person = { 'name':'wall', 'jser':true }; console.log("my name is "+ person.name); window["eval"]("console.log(\"I am Jser\");"); }
with 也适用上述的解决方案