UglifyJS 压缩工具

要使用 UglifyJS 进行 JavaScript 代码的压缩和混淆,你可以按照以下步骤进行:

  1. 安装 Node.js: 首先确保你的计算机上安装了 Node.js。如果没有安装,你可以从 Node.js 官方网站 下载并安装。

  2. 安装 UglifyJS: 在安装 Node.js 后,打开命令行工具(如命令提示符或终端)并运行以下命令来全局安装 UglifyJS:

npm install -g uglify-js

  1. 使用 UglifyJS: 在命令行中执行以下命令来压缩和混淆 JavaScript 代码:
 

uglifyjs main.js -o main.min.js -c -m

上述命令中的参数含义如下:

  • main.js:要压缩和混淆的输入文件。
  • -o main.min.js:输出压缩和混淆后的文件名。
  • -m:启用变量名混淆。
  • -c:启用代码压缩。

当我们有a.js b.js文件,b.js需要调用a.js中的文件,然后1.html上需要调用b.js方法的时候

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Calculate Distance</title>
  <script src="./ab.js"></script>
  <script src="./main.min.js"></script>
</head>
<body>
  <button onclick="clickFun()">点击</button>
  <p>Distance between coordinates (33, 36) and (116, 115): <span id="distance"></span> km</p>
  <script>
    function clickFun() {
      const distanceElement = document.getElementById('distance');
      const distance = space(33, 36, 116, 115);
      distanceElement.textContent = distance;
    }
  </script>
</body>
</html>

ab.js

function getCalculateDistance(k){return"我是ab.js中方法调用了"}

main.js    (main.js调用了ab.js)

function space(t, a, h, M) {
  t = t * Math.PI / 180, h = h * Math.PI / 180, a = a * Math.PI / 180 - M * Math.PI / 180, M = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((t - h) / 2), 2) + Math.cos(t) * Math.cos(h) * Math.pow(Math.sin(a / 2), 2)));
  return M *= 6378.137, (M = Math.round(1e4 * M) / 1e4) + getCalculateDistance()
}

 uglifyjs main.js -o main.min.js -c -m   压缩之后

function space(t,a,h,M){t=t*Math.PI/180,h=h*Math.PI/180,a=a*Math.PI/180-M*Math.PI/180,M=2*Math.asin(Math.sqrt(Math.pow(Math.sin((t-h)/2),2)+Math.cos(t)*Math.cos(h)*Math.pow(Math.sin(a/2),2)));return M*=6378.137,(M=Math.round(1e4*M)/1e4)+getCalculateDistance()}

点击按钮页面正常显示

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值