简体   繁体   English

混淆Javascript代码之前的先决条件?

[英]Prerequisite before obfuscating Javascript code?

I tried different obfuscation tools for JavaScript, but when I use resultant it does not work. 我为JavaScript尝试了不同的混淆工具,但是当我使用结果工具时,它不起作用。

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. 阅读有关使用该工具以确保其支持JavaScript实现的更多信息。

  • 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 = {}) 使用传入的对象创建任何全局公开的对象/方法(window.somename = {})
  • Use event binding, not on* attributes in your html markup. 在HTML标记中使用事件绑定,而不是on *属性。

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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