简体   繁体   中英

Prerequisite before obfuscating Javascript code?

I tried different obfuscation tools for JavaScript, but when I use resultant it does not work.

I removed all comments, single-line and multi-line comments, and beautified the code in order to have proper semi-colon and proper formatting.

My question is : What are the prerequisites before obfuscating the code?

If your code doesn't have any errors or warnings when debugging, then it should obfuscate. Read more about the tool you're using to make sure it supports your JavaScript implementation.

  • Make sure that your code runs inside an Immediately-Invoked Function Expression (see below example)
  • Do not rely on global variables (or explicitly pass them in as below for $)
  • Use the passed in object to create any globally exposed objects/methods (window.somename = {})
  • Use event binding, not on* attributes in your html markup.

If your code can run in relative isolation as below, it should be able to obfuscate very well...

Example:

(function(window, $){
  //your code here

  //create global namespace - expose classes/methods
  window.MyProject = {
    someMethod: someInternalMethodToExpose
    SomeClassName: SomeClass
  };

  //internal stuff
  function someInternalMethodToExpose() {
    ...
  }

  //constructor function
  function SomeClass() {
    ...
  }
  SomeClass.prototype.someMethod = function() {
    ...
  }
}(window || this, jQuery));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM