似乎没有办法完全隐藏源/加密某些内容以防止用户检查脚本背后的逻辑。
那么,除了查看源代码之外,是否可以在脚本运行时操纵每个变量、对象?这在某种程度上似乎是可能的:通过使用 Chrome 的开发者工具或 Firebug,您可以轻松地编辑变量,甚至在全局范围内调用函数。那么变量、实例化对象内部的函数或自调用匿名函数呢?这是一个例子:
var varInGlobal = 'On the global scope: easily editable';
function CustomConstructor()
{
this.exposedProperty = 'Once instantiated, can be easily manipulated too.';
this.func1 = function(){return func1InConstructor();}
var var1InConstructor = 'Can be retrived by invoking func1 from an instantiated object';
// Can it be assigned a new value after this is instantiated?
function func1InConstructor()
{
return var1InConstructor;
}
}
var customObject = new CustomConstructor();
在浏览器上运行之后:
// CONSOLE WINDOW
varInGlobal = 'A piece of cake!';
customObject.exposedProperty = 'Has new value now!';
customObject.var1InConstructor; // undefined: the variable can't be access this way
customObject.func1(); // This is the correct way
在这个阶段,用户是否可以在 customObject 中编辑变量“var1InConstructor”?
这是另一个例子:
There is a RPG game built on Javascript. The hero in the game has two stats: strength and agility. the character's final damage is calculated by combining these two stats. It is clear that players can find out this logic by inspecting the source.
Let's assume the entire script is self invoked and stats/calculate functions are inside of objects' constructors so they can't be reached by normally after instantiated. My question is, can the players edit the character's str and agi while the game is running(by using Firebug or whatever) so they can steamroll everything and ruin the game?