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 statement
传送门:官方文档
翻译成中文就是说:eval和with语句所处的作用域,YUI Compressor是不进行压缩混淆单词的。
1)源代码
function testCompressor(){
// 测试YUI Compressor
var person = {
'name':'wall',
'jser':true
};
console.log("my name is " person.name);
}
2)压缩后重新格式化的代码
function testCompressor() {
var a = {
name: "wall",
jser: true
};
console.log("my name is " a.name)
};
1)源代码
function testCompressor(){
// 测试YUI Compressor
var person = {
'name':'wall',
'jser':true
};
console.log("my name is " person.name);
eval("console.log(\"I am Jser\");");
}
2)压缩后重新格式化的代码
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)源代码
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"]进行调用
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 也适用上述的解决方案