我是谷歌闭包编译器的新手,在阅读了闭包工具站点中的文档后,我创建了一个js并进行了测试。
但是我发现即使我使用高级编译级别,编译的代码仍然很容易反编译。
例如,这是代码:
//2
window["_NameSpace_"]={};
window["_NameSpaceInternal_"]={};
(function() {
_NameSpaceInternal_.Class = function() {
var clazz = function() {
this["init"] && this["init"].apply(this, arguments);
};
var pros = {}, arg;
for(var c = 0, k = arguments.length; c < k; ++c) {
arg = arguments[c];
if( typeof arg === 'function') {
if(c == 0 && k > 1) {
var F = function() {
};
F.prototype = arg.prototype;
pros = new F;
arg = arg.prototype;
}
} else {
if(arg.init) {
clazz = arg["init"];
delete arg["init"]
}
}
}
for(var p in arg)
pros[p] = arg[p]
clazz.prototype = pros;
return clazz;
};
})();
(function(d){
d["Person"]=_NameSpaceInternal_.Class({
"init":function(name){
this.name=name;
},
"whatever":function(aaa){
}
});
})(_NameSpace_);
编译后(我为人类阅读制作了一个漂亮的格式(:
window.__MapNS__ = {};
window.__MapNSImpl__ = {};
__MapNSImpl__.a = function() {
function c() {
this.init && this.init.apply(this, arguments)
}
for (var b = {}, a, d = 0, e = arguments.length; d < e; ++d) if (a = arguments[d], "function" === typeof a) 0 == d && 1 < e && (b = function() {}, b.prototype = a.prototype, b = new b, a = a.prototype);
else {
a.b && (c = a.init, delete a.init)
}
if (!b || !a) throw Error("You must provide an object to copy from and to");
for (var f in a) b[f] = a[f];
c.prototype = b;
return c
};
(function(c) {
c.Person = __MapNSImpl__.a({
init: function(b) {
this.name = b
},
whatever:function(x){
}
})
})(__MapNS__);
现在,以"Person"的类定义为例,编译后,"Person"的所有方法显然都是针对黑客的,即使这些方法也必须暴露。
但我想知道我是否可以像这样制作编译的代码;
...........
var xx="init",xxx="whatever",xxxx=="Person";
c[xxxx] = __MapNSImpl__.a({
xx: function(b) {
this.name = b
},
xxx:function(x){
}
})
...........
就像谷歌地图的压缩一样。
知道吗?
您正在寻找的确切答案是通过使用 Java API 或创建自定义编译器来启用编译器中的别名传递。默认情况下不启用传递,因为在考虑 gzip 时,它往往会生成更大的代码。
要真正获得与大多数Google产品相同的效果,您需要对代码进行实质性更改。
-
在全局范围内定义对象和方法。在全局声明它们之后将它们分配给命名空间。可以使用
output_wrapper
标志将编译的代码包装在函数中,以防止全局范围冲突。例:function a() {}; window['Namespace']['obj'] = a;
-
直接定义对象 - 不要使用帮助程序方法。所以而不是
var a = _NameSpaceInternal_.Class({"init":function(name){ this.name=name; });
您可以使用类似以下内容:
function a(){}; a.prototype.name = ""; a.prototype.init = function(name) { this.name=name; };
这样可以避免使用带引号的语法,并允许编译器重命名属性和方法。
有关使用闭包编译器以最佳方式编译/重命名的编码风格的更多示例,请参阅闭包库。