Closure compiler - wrongly obfuscates class members

时间:2015-11-12 11:30:27

标签: javascript obfuscation google-closure-compiler

Im using closure compiler in advanced optimization mode to obfuscate javascript. I have few doubts regarding the obfuscation of class member variables. Here is a snippet of JS im trying to obfuscate.

function MyClass(){
   this.someFlag = false;
}
MyClass.prototype.myClassMethod = function (){
   if(this.someFlag) {
     console.log("Flag is true"); 
   }
}

I see inconsistent behaviour in obfuscation of class member variables by closure compiler. I have clearly described the inconsistency via the following 2 cases. Case 1: (Class member variable "someFlag" correctly obfuscated)

function a(){
  this.c = 0;
}
a.prototype.b = function(){
  if(this.c){
    console.log("Flag is true");
   }
}

Case 2: (Class member variable "someFlag" incorrectly obfuscated)

function a(){
  this.c = 0;//here someflag is obfuscated as c
}
a.prototype.b = function(){
  if(this.d){ //here someFlag is obfuscated as d
    console.log("Flag is true");
   }
}

Its not the same snippet of code that gets inconsistently obfuscated when running closure compiler on it. But similar pattern of Javascript code gets obfuscated in inconsistent manner. The current solution im using to prevent this inconsistency is to use quoted syntax for the impacted class variable. Like this: this["someFlag"] But using quoted syntax prevent obfuscating the class variable "someFlag". I want to obfuscate the class variables too. I want to know why closure compiler gets confused with class variables and incorrectly obfuscates in somecases ? Is using quoted syntax the only solution to avoid this inconsistency ? Is there anyother solution ?

0 个答案:

没有答案