{"id":8889,"date":"2023-09-01T17:11:53","date_gmt":"2023-09-01T09:11:53","guid":{"rendered":"\/?p=8889"},"modified":"2023-09-07T17:36:40","modified_gmt":"2023-09-07T09:36:40","slug":"ast%e8%87%aa%e5%8a%a8%e5%8c%96javascript%e9%98%b2%e6%8a%a4%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"\/?p=8889","title":{"rendered":"AST\u81ea\u52a8\u5316Javascript\u9632\u62a4\u65b9\u6848"},"content":{"rendered":"<h2>AST\u81ea\u52a8\u5316JavaScript\u9632\u62a4\u65b9\u6848<\/h2>\n<p>\u6ca1\u6709\u7279\u522b\u8bf4\u660e\u7684\u60c5\u51b5\u4e0b\uff0c\u90fd\u4ee5\u5904\u7406\u4ee5\u4e0b\u4ee3\u7801\u4e3a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">Date.prototype.format = function (formatStr){\n    var str = formatStr\n    var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;,&#039;\u4e8c&#039;,&#039;\u4e09&#039;,&#039;\u56db&#039;,&#039;\u4e94&#039;,&#039;\u516d&#039;]\n    str = str.replace(\/yyyy|YYYY\/, this.getFullYear())\n    str = str.replace(\/MM\/, (this.getMonth()+1)&gt;9 ? (this.getMonth()+1).toString() : &#039;0&#039; + (this.getMonth() + 1));\n    str = str.replace(\/dd|DD\/, this.getDate() &gt;9 ? this.getDate().toString() : &#039;0&#039; + this.getDate());\n    return str;\n}\nconsole.log(new Date().format(&#039;yyyy-MM-dd&#039;));<\/code><\/pre>\n<h2>\u4e00\u3001\u6df7\u6dc6\u524d\u7684\u4ee3\u7801\u5904\u7406<\/h2>\n<h3>1.\u6539\u53d8\u5bf9\u8c61\u5c5e\u6027\u7684\u8bbf\u95ee\u65b9\u5f0f<\/h3>\n<p>\u5bf9\u8c61\u5c5e\u6027\u8bbf\u95f4\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u5373\u7c7b\u4f3c console.log \u6216\u8005 console[&quot;log&quot;]\u3002\u5728JS \u6df7\u6dc6\u4e2d\u901a\u5e38\u4f1a\u4f7f\u7528\u7c7b\u4f3c\u8fd9\u79cd console[&quot;log&quot;]\u7684\u5bf9\u8c61\u5c5e\u6027\u8bbf\u95ee\u65b9\u5f0f\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230901095107854.png\" alt=\"image-20230901095107854\" \/><\/p>\n<p>\u90a3\u4e48\uff0c\u4e24\u79cd\u8bbf\u95ee\u65b9\u5f0f\u5728 AST \u8282\u70b9\u4e2d\u53c8\u6709\u4ec0\u4e48\u533a\u522b\u5462?<\/p>\n<p>\u4e0b\u9762\u4ecb\u7ecd\u4e24\u79cd\u8bbf\u95ee\u65b9\u5f0f\u5728AST\u8282\u70b9\u4e2d\u7684\u533a\u522b\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">traverse(ast, {\n    MemberExpression(path) {\n        console.log(path.node)\n    }\n});<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230901100237688.png\" alt=\"image-20230901100237688\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u51fa\u4e3b\u8981\u6709\u4e24\u5904\u4e0d\u4e00\u6837:<\/p>\n<p>console.log \u8fd9\u79cd\u8bbf\u95ee\u65b9\u5f0f\u7684 property \u4e3a Identifier,computed\u4e3afalse\u3002<\/p>\n<p>console[&quot;log&quot;]\u8fd9\u79cd\u8bbf\u95ee\u5f0fproperty\u4e3aStringLiteral\uff0ccomputed\u4e3a true\u3002<\/p>\n<p>\u53ef\u4ee5\u518d\u770b\u4e00\u4e2a\u6bd4\u8f83\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230907154556280.png\" alt=\"image-20230907154556280\" \/><\/p>\n<p>\u56e0\u6b64\uff0c\u53ea\u8981\u904d\u5386MemberExpression (\u6210\u5458\u8868\u8fbe\u5f0f)\uff0c\u628a\u5bf9\u5e94\u7684\u5c5e\u6027\u4fee\u6539\u6389\u5373\u53ef\u5b8c\u6210\u4e24\u79cd\u8bbf\u95f4\u65b9\u5f0f\u7684\u8f6c\u6362\u3002<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    MemberExpression(path){\n        if(t.isIdentifier(path.node.property)){\n            let name = path.node.property.name;\n            path.node.property = t.stringLiteral(name);\n        }\n        \/\/*\u8fd9\u884c\u4ee3\u7801\u5fc5\u52a0\n        path.node.computed = true;\n    }, \n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u6df7\u6dc6\u4e4b\u524d\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u80fd\u4e24\u79cd\u8bbf\u95ee\u65b9\u5f0f\u90fd\u6709\uff0c\u4e0d\u4e00\u5b9a\u90fd\u662f console.log\u8fd9\u79cd\u65b9\u5f0f\u56e0\u6b64\u4fee\u6539property \u5c5e\u6027\u7684\u65f6\u5019\u8981\u5224\u65ad\u4e00\u4e0b\uff0c\u53ea\u5904\u7406\u7c7b\u578b\u4e3a Identifier \u7684\u60c5\u51b5\u5373\u53ef\u3002<\/p>\n<p><code>path.node.computed = true;<\/code>\u4e0d\u52a0\u540e\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230907155613681.png\" alt=\"image-20230907155613681\" \/><\/p>\n<p>\u6b64\u523b\u8239\u65b0\u7248\u672c\uff1a<\/p>\n<pre><code class=\"language-javascript\">Date.prototype.format = function (formatStr){\n    var str = formatStr\n    var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;,&#039;\u4e8c&#039;,&#039;\u4e09&#039;,&#039;\u56db&#039;,&#039;\u4e94&#039;,&#039;\u516d&#039;]\n    str = str.replace(\/yyyy|YYYY\/, this.getFullYear())\n    str = str.replace(\/MM\/, (this.getMonth()+1)&gt;9 ? (this.getMonth()+1).toString() : &#039;0&#039; + (this.getMonth() + 1));\n    str = str.replace(\/dd|DD\/, this.getDate() &gt;9 ? this.getDate().toString() : &#039;0&#039; + this.getDate());\n    return str;\n}\nconsole.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n\/*\n\u53d8\u5316\u5982\u4e0b\uff1a\n*\/\nDate[&quot;prototype&quot;][&quot;format&quot;] = function (formatStr) {\n  var str = formatStr;\n  var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;];\n  str = str[&quot;replace&quot;](\/yyyy|YYYY\/, this[&quot;getFullYear&quot;]());\n  str = str[&quot;replace&quot;](\/MM\/, this[&quot;getMonth&quot;]() + 1 &gt; 9 ? (this[&quot;getMonth&quot;]() + 1)[&quot;toString&quot;]() : &#039;0&#039; + (this[&quot;getMonth&quot;]() + 1));\n  str = str[&quot;replace&quot;](\/dd|DD\/, this[&quot;getDate&quot;]() &gt; 9 ? this[&quot;getDate&quot;]()[&quot;toString&quot;]() : &#039;0&#039; + this[&quot;getDate&quot;]());\n  return str;\n};\nconsole[&quot;log&quot;](new Date()[&quot;format&quot;](&#039;yyyy-MM-dd&#039;));<\/code><\/pre>\n<p>\u52a0\u5bc6\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n    encoding: &quot;utf-8&quot;\n});\nlet ast = parser.parse(jscode);\n\n\/\/ 1.\u6539\u53d8\u5bf9\u8c61\u5c5e\u6027\u7684\u8bbf\u95ee\u65b9\u5f0f console.log =&gt; console[&#039;log&#039;]\ntraverse(ast, {\n    MemberExpression: function (path) {\n        if(t.isIdentifier(path.node.property)){\n            let name = path.node.property.name;\n            path.node.property = t.stringLiteral(name);\n        }\n        path.node.computed = true\n    }\n})\n\/\/ \u66f4\u65b0AST \u2014\u2014\u4f3c\u4e4e\u4e5f\u4e0d\u9700\u8981\u66f4\u65b0\uff0c\u5df2\u7ecf\u88ab\u904d\u5386\u8fc7\u4e86\n\/\/ ast = generatorObj(ast);\n\nlet code = generator(ast).code;\nconsole.log(code)\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});<\/code><\/pre>\n<h3>2.JS\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\u7684\u5904\u7406<\/h3>\n<p>\u5728\u7ecf\u8fc7\u6539\u53d8\u8bbf\u95ee\u65b9\u5f0f\u540e\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\u6709\u4e24\u4e2a Date\uff0c\u8fd9\u662fJS \u4e2d\u7684\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\uff0c\u8bfb\u8005\u4eec\u4e5f\u53ef\u4ee5\u7406\u89e3\u6210\u662f\u7cfb\u7edf\u51fd\u6570\u3002\u4e5f\u5c31\u662f\u60f3\u8981\u4f7f\u7528\u5c31\u5fc5\u987b\u662f\u8fd9\u4e2a\u540d\u5b57\uff0c\u5fc5\u987b\u8fd9\u4e48\u5199\uff0c\u90a3\u662f\u5426\u5c31\u4e0d\u80fd\u5bf9Date\u8fdb\u884c\u52a0\u5bc6\u4e86\u5462?\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u5185\u7f6e\u5bf9\u8c61\u4e00\u822c\u90fd\u662f window \u4e0b\u7684\u5c5e\u6027\u56e0\u6b64\u53ef\u4ee5\u8f6c\u6210window[&quot;Date&quot;]\u8fd9\u79cd\u5f62\u5f0f\u6765\u8bbf\u95eeDate\uff0c\u8fd9\u6837Date \u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5c31\u53ef\u4ee5\u8fdb\u884c\u52a0\u5bc6\u4e86\u3002\u53e6\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7cfb\u7edf\u81ea\u5e26\u7684\u5168\u5c40\u51fd\u6570\uff0c\u5b83\u4eec\u4e5f\u5c31\u662f window \u4e0b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4e00\u5e76\u5904\u7406\u3002<\/p>\n<p>JS \u53d1\u5c55\u5230\u73b0\u5728\uff0c\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\u5df2\u7ecf\u6709\u5f88\u591a\u4e86\u3002\u7b14\u8005\u5728\u8fd9\u91cc\u53ea\u6f14\u793a\u5176\u4e2d\u4e00\u90e8\u5206\uff0c\u8bfb\u8005\u4eec\u53ef\u81ea\u884c\u767e\u5ea6\u201cJS \u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\u201d\u83b7\u53d6\u66f4\u5168\u7684\u3002<\/p>\n<p>JS \u4e2d\u7684\u5e38\u89c1\u7684\u5168\u5c40\u51fd\u6570\u6709 eval\u3001parseInt\u3001encodeURIComponent\u3002<\/p>\n<p>JS \u4e2d\u5e38\u89c1\u7684\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\u6709 0bject\u3001Function\u3001Boolean\u3001Number\u3001Math\u3001Date\u3001String\u3001RegExp\u3001Array \u7b49\u3002<\/p>\n<p>\u8fd9\u4e9b\u5168\u5c40\u51fd\u6570\u548c\u5185\u7f6e\u5bf9\u8c61\u5728AST \u4e2d\u90fd\u662f\u6807\u8bc6\u7b26\uff0c\u5e76\u4e14\u90fd\u662f window \u5bf9\u8c61\u4e0b\u7684\u3002\u56e0\u6b64\u5904\u7406\u4ee3\u7801\u7684\u601d\u8def\u5c31\u6765\u4e86\uff0c\u904d\u5386 Identifier \u8282\u70b9\uff0c\u628a\u8be5\u8282\u70b9\u66ff\u6362\u6210object \u5c5e\u6027\u4e3a window \u7684MemberExpression \u5373\u53ef\u3002<\/p>\n<p>\u610f\u601d\u662f\uff0c\u54b1\u628a\u8fd9\u4e2a\u6210\u5458\u8868\u8fbe\u5f0fMemberExpression \u518d\u52a0\u4e00\u5c42\uff0cconsole.log\u53d8\u6210\u4e86window.console.log\uff0ccallee\u7684\u5de6\u8fb9\uff0c\u5c31\u662f\u8fd9\u4e2a<code>.<\/code>\u7684\u5de6\u4fa7\u7684\u662fobject\uff0c\u53f3\u4fa7\u662fproperty\u3002\u5982\u679c\u628awindow.console\u770b\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u4ed6\u4eec\u5c31\u4f5c\u4e3a\u4e00\u4e2aMemberExpression\u5728object\u91cc\u9762\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230907163056814.png\" alt=\"image-20230907163056814\" \/><\/p>\n<p>\u4e5f\u5c31\u662f\u9700\u8981\u751f\u6210MemberExpression\uff0c\u5b83\u5728 ts \u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u4e3a:<\/p>\n<pre><code class=\"language-typescript\">export function memberExpression(object: Expression, property: any, computed?:boolean, optional?: true | false | null): MemberExpression;<\/code><\/pre>\n<p>\u4f20\u5165 object\u3001property\u3001computed\uff0c\u8fd4\u56de MemberExpression\u3002\u5185\u7f6e\u5bf9\u8c61\u7684\u5904\u7406\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    Identifier(path){\n        let name = path.node.name;\n        if(&#039;console|eval|parseInt|encodeURIComponent|Object|Function|Boolean|Number|Math|Date|String|RegExp|Array&#039;.indexOf(name) != -1){\n            path.replaceWith(t.memberExpression(t.identifier(&#039;window&#039;), t.stringLiteral(name), true));\n        }\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u5148\u83b7\u53d6\u5230 Identifier \u7684 name \u5c5e\u6027\uff0c\u7136\u540e\u5230\u9884\u5148\u51c6\u5907\u597d\u7684\u5b57\u7b26\u4e32\u4e2d\u53bb\u641c\u7d22\u3002\u5982\u679c\u662f\u5185\u7f6e\u5bf9\u8c61\u6216\u5168\u5c40\u51fd\u6570\uff0c\u5c31\u628a\u5b83\u66ff\u6362\u6210 object \u5c5e\u6027\u4e3a window \u7684MemberExpression<\/p>\n<p>\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">window[&quot;Date&quot;][&quot;prototype&quot;][&quot;format&quot;] = function (formatStr) {\n  var str = formatStr;\n  var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;];\n  str = str[&quot;replace&quot;](\/yyyy|YYYY\/, this[&quot;getFullYear&quot;]());\n  str = str[&quot;replace&quot;](\/MM\/, this[&quot;getMonth&quot;]() + 1 &gt; 9 ? (this[&quot;getMonth&quot;]() + 1)[&quot;toString&quot;]() : &#039;0&#039; + (this[&quot;getMonth&quot;]() + 1));\n  str = str[&quot;replace&quot;](\/dd|DD\/, this[&quot;getDate&quot;]() &gt; 9 ? this[&quot;getDate&quot;]()[&quot;toString&quot;]() : &#039;0&#039; + this[&quot;getDate&quot;]());\n  return str;\n};\nwindow[&quot;console&quot;][&quot;log&quot;](new window[&quot;Date&quot;]()[&quot;format&quot;](&#039;yyyy-MM-dd&#039;));<\/code><\/pre>\n<h2>\u4e8c\u3001\u5e38\u91cf\u4e0e\u6807\u8bc6\u7b26\u7684\u6df7\u6dc6<\/h2>\n<h3>1.\u5b9e\u73b0\u6570\u503c\u5e38\u91cf\u52a0\u5bc6<\/h3>\n<p>\u5f02\u6216\u8fd0\u7b97\u7684\u89c4\u5f8b(\u4e92\u4e3a\u5f02\u6216\u5173\u7cfb)\uff1a<\/p>\n<pre><code>A ^ B = C\nC ^ A = B\nC ^ B = A<\/code><\/pre>\n<p>\u4ee3\u7801\u4e2d\u7684\u6570\u503c\u5e38\u91cf\u53ef\u4ee5\u901a\u8fc7\u904d\u5386 NumericLiteral \u8282\u70b9\uff0c\u83b7\u53d6\u5176\u4e2d\u7684 value \u5c5e\u6027\u6765\u5f97\u5230\u3002<\/p>\n<p>\u7136\u540e\u968f\u673a\u751f\u6210\u4e00\u4e2a\u6570\u503c\u8bb0\u4e3a key\u3002\u63a5\u7740\u628a value \u4e0e key \u8fdb\u884c\u5f02\u6216\u5f97\u5230\u52a0\u5bc6\u540e\u7684\u6570\u503c\u8bb0\u4e3acipherNum\uff0c\u5373 cipherNum = value  key\u3002<\/p>\n<p>\u6b64\u65f6\uff0cvalue = cipherNum^key\u3002\u56e0\u6b64\u53ef\u4ee5\u751f\u6210\u4e00\u4e2abinaryExpression \u8282\u70b9\u6765\u7b49\u4ef7\u7684\u66ff\u6362 NumericLiteral \u8282\u70b9\u3002<\/p>\n<p>binaryExpression\u7684operator \u4e3a\uff0cleft\u4e3a cipherNum\uff0cright\u4e3a key\u3002\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    NumericLiteral(path){\n        let value = path.node.value;\n        let key = parseInt(Math.random() * (999999 - 100000) + 100000, 10);\n        let cipherNum = value ^ key;\n        path.replaceWith(t.binaryExpression(&#039;^&#039;, t.numericLiteral(cipherNum), t.numericLiteral(key)));\n        \/\/\u66ff\u6362\u540e\u7684\u8282\u70b9\u91cc\u4e5f\u6709numericLiteral\u8282\u70b9\uff0c\u4f1a\u9020\u6210\u6b7b\u5faa\u73af\uff0c\u56e0\u6b64\u9700\u8981\u52a0\u5165path.skip()\n        path.skip();\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\nDate.prototype.format = function (formatStr) {\n  var str = formatStr;\n  var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;];\n  str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n  str = str.replace(\/MM\/, this.getMonth() + (983778 ^ 983779) &gt; (474226 ^ 474235) ? (this.getMonth() + (365297 ^ 365296)).toString() : &#039;0&#039; + (this.getMonth() + (381808 ^ 381809)));\n  str = str.replace(\/dd|DD\/, this.getDate() &gt; (119081 ^ 119072) ? this.getDate().toString() : &#039;0&#039; + this.getDate());\n  return str;\n};\nconsole.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n*\/<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230901104306098.png\" alt=\"image-20230901104306098\" \/><\/p>\n<h3>2.\u5b9e\u73b0\u5b57\u7b26\u4e32\u5e38\u91cf\u52a0\u5bc6<\/h3>\n<p>\u539f\u59cb\u4ee3\u7801\u7ecf\u8fc7\u524d\u51e0\u4e2a\u5c0f\u8282\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u770b\u51fa\u5f88\u591a\u6807\u8bc6\u7b26\u90fd\u53d8\u6210\u4e86\u5b57\u7b26\u4e32\u3002\u4f46\u662f\u8fd9\u4e9b\u5b57\u7b26\u4e32\u4f9d\u7136\u662f\u660e\u6587\uff0c\u8fd8\u662f\u4e00\u773c\u5c31\u80fd\u770b\u660e\u767d\u3002\u56e0\u6b64\u8fd9\u4e00\u5c0f\u8282\u5c31\u8981\u5bf9\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8fdb\u884c\u52a0\u5bc6\uff0c\u52a0\u5bc6\u7684\u601d\u8def\u5f88\u7b80\u5355\u3002\u6bd4\u5982\uff0c\u4ee3\u7801\u4e2d\u660e\u6587\u662f&quot;prototype&quot;\uff0c\u52a0\u5bc6\u540e\u7684\u503c\u4e3a&quot;cHJvdG90exBI&quot;\uff0c\u89e3\u5bc6\u51fd\u6570\u540d\u4e3aatob\u3002\u90a3\u4e48\u53ea\u8981\u6784\u5efa\u4e00\u4e0batob(&quot;cHJvdG90eXB1&quot;)\uff0c\u7136\u540e\u7528\u5b83\u666e\u6362\u539f\u5148\u7684&quot;prototype&quot;\u7532\u53ef\u3002<\/p>\n<p>\u5f53\u7136\u89e3\u5bc6\u51fd\u6570\u4e5f\u9700\u8981\u4e00\u8d77\u653e\u5165\u539f\u59cb\u4ee3\u7801\u4e2d\u3002\u7b14\u8005\u5728\u8fd9\u91cc\u5c31\u7528 Base64\u7f16\u7801\u4e00\u4e0b\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u6d4f\u89c8\u5668\u81ea\u5e26\u7684atob \u6765\u89e3\u5bc6\u3002<\/p>\n<p>\u90a3\u4e48\u5728AST\u4e2d\u64cd\u4f5c\u7684\u8bdd\uff0c\u5c31\u8981\u5148\u904d\u5386\u6240\u6709\u7684 StringLiteral\uff0c\u53d6\u51fa\u5176\u4e2d\u7684value \u5c5e\u6027\u8fdb\u884c\u52a0\u5bc6\uff0c\u7136\u540e\u628aStringliteral\u8282\u70b9\u66ff\u6362\u4e3acallExpression (\u51fd\u6570\u8c03\u7528\u8868\u8fbe\u5f0f)\u3002\u5982\u4f55\u751f\u6210callExpression \u6635?\u6765\u770b\u4e00\u4e0bcallExpression\u5728ts \u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49:<\/p>\n<pre><code class=\"language-typescript\">export function callExpression(callee: Expression | V8IntrinsicIdentifiex, _arguments: Array&lt;Expression | SpreadElement |  JSXNamespacedName | ArgumentPlaceholder&gt;): CalIExpression;<\/code><\/pre>\n<p>callee\u8868\u793a\u51fd\u6570\u540d\uff0c\u4f20\u5165\u4e00\u4e2aIdentifier \u5373\u53ef\u3002_arguments\u8868\u793a\u53c2\u6570\uff0c\u53c2\u6570\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u56e0\u6b64\u5b83\u662f\u4e00\u4e2a\u6570\u7ec4\u3002\u51fd\u6570\u6700\u7ec8\u8fd4\u56de CallExpression\u3002\u5b9e\u73b0\u5b57\u7b26\u4e32\u5e38\u91cf\u52a0\u5bc6\u7684\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\nfunction base64Encode(e) {\n    var r, a, c, h, o, t, base64EncodeChars = &#039;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/&#039;;\n    for (c = e.length, a = 0, r = &#039;&#039;; a &lt; c;) {\n        if (h = 255 &amp; e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4),\n            r += &#039;==&#039;;\n            break\n        }\n        if (o = e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n            r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2),\n            r += &#039;=&#039;;\n            break\n        }\n        t = e.charCodeAt(a++),\n        r += base64EncodeChars.charAt(h &gt;&gt; 2),\n        r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n        r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2 | (192 &amp; t) &gt;&gt; 6),\n        r += base64EncodeChars.charAt(63 &amp; t)\n    }\n    return r\n}\n\ntraverse(ast, {\n    StringLiteral(path){\n        \/\/\u751f\u6210callExpression\u53c2\u6570\u5c31\u662f\u5b57\u7b26\u4e32\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\n        let encStr = t.callExpression(\n                 t.identifier(&#039;atob&#039;),\n                 [t.stringLiteral(base64Encode(path.node.value))]);\n        path.replaceWith(encStr);\n        \/\/\u66ff\u6362\u540e\u7684\u8282\u70b9\u91cc\u4e5f\u6709StringLiteral\u8282\u70b9\uff0c\u4f1a\u9020\u6210\u6b7b\u5faa\u73af\uff0c\u56e0\u6b64\u9700\u8981\u52a0\u5165path.skip()\n        path.skip();\n  }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\nDate.prototype.format = function (formatStr) {\n  var str = formatStr;\n  var Week = [atob(&quot;5Q==&quot;), atob(&quot;AA==&quot;), atob(&quot;jA==&quot;), atob(&quot;CQ==&quot;), atob(&quot;2w==&quot;), atob(&quot;lA==&quot;), atob(&quot;bQ==&quot;)];\n  str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n  str = str.replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : atob(&quot;MA==&quot;) + (this.getMonth() + 1));\n  str = str.replace(\/dd|DD\/, this.getDate() &gt; 9 ? this.getDate().toString() : atob(&quot;MA==&quot;) + this.getDate());\n  return str;\n};\nconsole.log(new Date().format(atob(&quot;eXl5eS1NTS1kZA==&quot;)));\n*\/<\/code><\/pre>\n<h3>3.\u5b9e\u73b0\u6570\u7ec4\u6df7\u6dc6<\/h3>\n<p>\u6765\u89c2\u5bdf\u4e00\u4e0b\u7ecf\u8fc7\u5b57\u7b26\u4e32\u52a0\u5bc6\u4ee5\u540e\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u5b57\u7b26\u4e32\u867d\u7136\u52a0\u5bc6\u4e86\uff0c\u4f46\u662f\u5b57\u7b26\u4e32\u4f9d\u7136\u5728\u539f\u5148\u7684\u4f4d\u7f6e\u3002\u6570\u7ec4\u6df7\u6dc6\u8981\u505a\u7684\u4e8b\u60c5\uff0c\u5c31\u662f\u628a\u8fd9\u4e9b\u5b57\u7b26\u4e32\u90fd\u63d0\u53d6\u5230\u6570\u7ec4\u4e2d\uff0c\u539f\u5148\u5b57\u7b26\u4e32\u7684\u5730\u65b9\u6539\u4e3a\u4ee5\u6570\u7ec4\u4e0b\u6807\u7684\u65b9\u5f0f\u53bb\u8bbf\u95ee\u6570\u7ec4\u6210\u5458\u3002\u5176\u5b9e\u8fd8\u53ef\u4ee5\u63d0\u53d6\u5230\u591a\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4e0d\u540c\u7684\u6570\u7ec4\u5904\u4e8e\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u4e2d\u3002\u7b14\u8005\u5728\u8fd9\u91cc\u5c31\u53ea\u5b9e\u73b0\u4e00\u4e0b\u63d0\u53d6\u5230\u4e00\u4e2a\u6570\u7ec4\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982 Datelatob(&quot;cHJvdg90eXB1&quot;)\uff0c\u628a&quot;cHJvdG90eXB1&quot;\u53d8\u6210\u6570\u7ec4 arr \u7684\u4e0b\u6807\u4e3a 0 \u7684\u6210\u5458\u540e\uff0c\u539f\u5148\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\u5c31\u53d8\u4e3a\uff0cDate[atob(arr[0])],\u5f53\u7136\u8fd8\u9700\u8981\u989d\u5916\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u653e\u5165\u5230\u88ab\u6df7\u6dc6\u7684\u4ee3\u7801\u4e2d\u3002<\/p>\n<p>\u90a3\u4e48AST \u600e\u4e48\u6765\u5904\u7406\u5462?\u9996\u5148\u80af\u5b9a\u662f\u904d\u5386 StringLiteral \u8282\u70b9\u3002\u65e2\u7136\u8fd9\u4e2a\u4e5f\u662f\u904d\u5386StringLiteral\u8282\u70b9\uff0c\u5176\u5b9e\u5c31\u53ef\u4ee5\u548c\u5b57\u7b26\u4e32\u52a0\u5bc6\u4e00\u8d77\u5904\u7406\u4e86\u3002\u5148\u6765\u6784\u9020\u51fa\u8fd9\u4e48\u4e00\u4e2a\u7ed3\u6784<br \/>\natob(arr[0])\uff0c\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">\/\/CallExpression:\u51fd\u6570\u8c03\u7528\u8868\u8fbe\u5f0f\nlet encstr = t.cal1Expression(\n        t.identifier(&#039;atob&#039;),\n        [t.memberExpression(t.identifier(&#039;arr&#039;),t.numericLiteral(index),\ntrue)]);<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u6784\u5efa\u4e86\u4e00\u4e2a callExpression\uff0c\u51fd\u6570\u540d\u4e3aatob\uff0c\u53c2\u6570\u662f memberExpression\u3002\u8fd9\u4e2amemberExpression \u7684 object \u5c5e\u6027\u4e3a arr (\u5c31\u662f\u5f85\u4f1a\u8981\u751f\u6210\u7684\u6570\u7ec4\u540d)\uff0cproperty \u5c5e\u6027\u4e3a\u6570\u7ec4\u7d22\u5f15 index\uff0c\u8fd9\u4e2a\u503c\u5f53\u524d\u672a\u77e5\u3002<\/p>\n<p>\u63a5\u7740\u8981\u628a\u4ee3\u7801\u4e2d\u7684\u5b57\u7b26\u4e32\u653e\u5165\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u5f97\u5230\u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd9\u91cc\u6709\u4e24\u79cd\u65b9\u6848\uff1a<\/p>\n<ol>\n<li>\u7b2c\u4e00\u79cd\u662f\u4e0d\u7ba1\u5b57\u7b26\u4e32\u6709\u6ca1\u6709\u91cd\u590d\uff0c\u904d\u5386\u5230\u4e00\u4e2a\u5c31\u5f80\u6570\u7ec4\u91cc\u653e\uff0c\u7136\u540e\u628a\u65b0\u7684\u7d22\u5f15\u8d4b\u503c\u7ed9\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 index\u3002<\/li>\n<li>\u7b2c\u4e8c\u79cd\u662f\u5b57\u7b26\u4e32\u653e\u5165\u6570\u7ec4\u4e4b\u524d\uff0c\u5148\u67e5\u8be2\u4e00\u4e0b\u6570\u7ec4\u4e2d\u6709\u6ca1\u6709\u76f8\u540c\u7684\uff0c\u5982\u679c\u6709\u76f8\u540c\u7684\u8bdd\uff0c\u5f97\u5230\u539f\u5148\u90a3\u4e2a\u7d22\u5f15\u8d4b\u503c\u7ed9 index\u3002\u7b14\u8005\u5728\u8fd9\u91cc\u91c7\u7528\u7b2c\u4e8c\u79cd\uff0c\u4ee3\u7801\u5982\u4e0b:<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">function base64Encode(str) {\n    ...\n}\ntraverse(ast, {\n    \/\/ StringLiteral \u904d\u5386\u5b57\u7b26\u4e32\u5e38\u91cf\u8282\u70b9[&quot;aa&quot;][&quot;bb&quot;]\n    StringLiteral(path){\n        let cipherText = base64Encode(path.node.value);\n        \/\/\u5728JS \u4e2d\uff0cindexof \u4e0d\u53ea\u662f\u53ef\u4ee5\u7528\u6765\u641c\u7d22\u5b57\u7b26\u4e32\uff0c\u5b9e\u9645\u4e0a\u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u641c\u7d22\u6570\u7ec4\u4e2d\u67d0\u4e2a\u6210\u5458\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u8fd4\u56de\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u8fd4 -1\n        let bigArrIndex = bigArr.indexOf(cipherText);\n        let index = bigArrIndex;\n        if(bigArrIndex == -1){\n            let length = bigArr.push(cipherText);\n            index = length -1;\n        }\n        let encStr = t.callExpression(\n                    t.identifier(&#039;atob&#039;), \n                    [t.memberExpression(t.identifier(&#039;arr&#039;), \n                                     t.numericLiteral(index), true)]);\n        path.replaceWith(encStr);\n    }\n});<\/code><\/pre>\n<p>\u5728JS \u4e2d\uff0cindexof \u4e0d\u53ea\u662f\u53ef\u4ee5\u7528\u6765\u641c\u7d22\u5b57\u7b26\u4e32\uff0c\u5b9e\u9645\u4e0a\u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u641c\u7d22\u6570\u7ec4\u4e2d\u67d0\u4e2a\u6210\u5458\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u8fd4\u56de\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u8fd4 -1<\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cpath.node.value \u5c31\u662f\u660e\u6587\u5b57\u7b26\u4e32\uff0c\u5148\u8fdb\u884c\u52a0\u5bc6\u5f97\u5230 cipherText\uff0c\u518d\u53bb\u6570\u7ec4\u4e2d\u641c\u7d22\u662f\u5426\u5b58\u5728\u4e0e cipherText \u4e00\u6837\u7684\u6210\u5458\u3002\u5982\u679c\u5df2\u7ecf\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\uff0c\u90a3\u5c31\u628a\u539f\u5148\u7684\u7d22\u5f15\u8d4b\u503c\u7ed9 index\uff0c\u7136\u540e\u6784\u5efa\u51fd\u6570\u8c03\u7528\u8868\u8fbe\u5f0f\uff0c\u66ff\u6362\u5f53\u524d\u7684 StringLiteral \u8282\u70b9\u3002<\/p>\n<p>\u7528\u4e8e\u66ff\u6362\u7684\u8282\u70b9\u4e2d\u6ca1\u6709 StringLiteral \u8282\u70b9\uff0c\u4e0d\u4f1a\u6b7b\u5faa\u73af\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u52a0\u5165\u505c\u6b62\u904d\u5386\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\uff0c\u4e5f\u5c31\u662findex0f \u8fd4\u56de\u7684\u7ed3\u679c\u4e3a -1\uff0c\u5c31\u628a cipherText \u52a0\u5165\u5230\u6570\u7ec4\u4e2d\u3002<\/p>\n<p>\u6570\u7ec4\u7684push \u65b9\u6cd5\uff0c\u53ef\u4ee5\u540c\u65f6\u52a0\u5165\u591a\u4e2a\u6210\u5458\uff0c\u9664\u4e86\u52a0\u5165\u6210\u5458\u5230\u6570\u7ec4\u672b\u5c3e\u4ee5\u5916\uff0c\u8fd8\u4f1a\u8fd4\u56de\u52a0\u5165\u6210\u5458\u4ee5\u540e\u7684\u6570\u7ec4\u957f\u5ea6\uff0c\u56e0\u6b64length - 1\u5c31\u662f\u6570\u7ec4\u6700\u672b\u6210\u5458\u7684\u7d22\u5f15\u3002<\/p>\n<p>\u5f53\u524dbigArr\u4e2d\u7684\u6210\u5458\u53ea\u662fJS\u4e2d\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u4e0d\u662fAST\u4e2d\u9700\u8981\u7684StringLiteral\u8282\u70b9\u3002\u56e0\u6b64\u8fd8\u9700\u8981\u505a\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u628abigArr\u4e2d\u7684\u6210\u5458\u90fd\u8f6c\u6210stringLiteral\u8282\u70b9\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">bigArr = bigArr.map(function(v){\n    return t.stringLiteral(v);\n})<\/code><\/pre>\n<p>\u73b0\u5728\u628abigArr\u52a0\u5165\u5230ast\u4e2d\uff0c\u4e5f\u5c31\u662f\u8981\u5148\u7528types\u7ec4\u4ef6\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\u58f0\u660e\uff0c\u5e76\u4e14\u6570\u7ec4\u6210\u5458\u4e0ebigArr\u4e00\u81f4\uff0c\u7136\u540e\u52a0\u5165\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u7684\u6700\u4e0a\u9762\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">bigArr = t.variableDeclarator(t.indentifier(&#039;arr&#039;), t.arrayExpression(bigArr));\nbigArr = t.variableDeclarator(&#039;var&#039;, [bigArr]);<\/code><\/pre>\n<p>program\u8282\u70b9\u76f8\u9047\u6574\u4e2aJS\u6587\u4ef6\uff0c\u5b83\u7684body\u5c5e\u6027\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u7528unshift\u628abigArr\u52a0\u5165\u5230\u6700\u524d\u9762\u3002\u5b8c\u6574\u7684\u6570\u7ec4\u6df7\u6dc6\u5904\u7406\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\nfunction base64Encode(e) {\n    var r, a, c, h, o, t, base64EncodeChars = &#039;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/&#039;;\n    for (c = e.length, a = 0, r = &#039;&#039;; a &lt; c;) {\n        if (h = 255 &amp; e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4),\n            r += &#039;==&#039;;\n            break\n        }\n        if (o = e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n            r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2),\n            r += &#039;=&#039;;\n            break\n        }\n        t = e.charCodeAt(a++),\n        r += base64EncodeChars.charAt(h &gt;&gt; 2),\n        r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n        r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2 | (192 &amp; t) &gt;&gt; 6),\n        r += base64EncodeChars.charAt(63 &amp; t)\n    }\n    return r\n}\n\nlet bigArr = [];\ntraverse(ast, {\n    StringLiteral(path){\n        let cipherText = base64Encode(path.node.value);\n        let bigArrIndex = bigArr.indexOf(cipherText);\n        let index = bigArrIndex;\n        if(bigArrIndex == -1){\n            let length = bigArr.push(cipherText);\n            index = length -1;\n        }\n        let encStr = t.callExpression(\n                    t.identifier(&#039;atob&#039;),\n                    [t.memberExpression(t.identifier(&#039;arr&#039;),\n                                     t.numericLiteral(index), true)]);\n        path.replaceWith(encStr);\n    }\n});\nbigArr = bigArr.map(function(v){\n  return t.stringLiteral(v);\n});\nbigArr = t.variableDeclarator(t.identifier(&#039;arr&#039;), t.arrayExpression(bigArr));\nbigArr = t.variableDeclaration(&#039;var&#039;, [bigArr]);\nast.program.body.unshift(bigArr);\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\nvar arr = [&quot;5Q==&quot;, &quot;AA==&quot;, &quot;jA==&quot;, &quot;CQ==&quot;, &quot;2w==&quot;, &quot;lA==&quot;, &quot;bQ==&quot;, &quot;MA==&quot;, &quot;eXl5eS1NTS1kZA==&quot;];\nDate.prototype.format = function (formatStr) {\n  var str = formatStr;\n  var Week = [atob(arr[0]), atob(arr[1]), atob(arr[2]), atob(arr[3]), atob(arr[4]), atob(arr[5]), atob(arr[6])];\n  str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n  str = str.replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : atob(arr[7]) + (this.getMonth() + 1));\n  str = str.replace(\/dd|DD\/, this.getDate() &gt; 9 ? this.getDate().toString() : atob(arr[7]) + this.getDate());\n  return str;\n};\nconsole.log(new Date().format(atob(arr[8])));\n*\/<\/code><\/pre>\n<h3>4.\u5b9e\u73b0\u6570\u7ec4\u4e71\u5e8f<\/h3>\n<p>\u7ecf\u8fc7\u6570\u7ec4\u6df7\u6dc6\u4ee5\u540e\u7684\u4ee3\u7801\uff0c\u5f15\u7528\u5904\u7684\u6570\u7ec4\u7d22\u5f15\u4e0e\u539f\u5148\u5b57\u7b26\u4e32\u8fd8\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u3002\u8fd9\u4e00\u5c0f\u8282\u5c31\u8981\u6253\u4e71\u8fd9\u4e2a\u6570\u7ec4\u7684\u987a\u5e8f\u3002\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u4f20\u4e00\u4e2a\u6570\u7ec4\u8fdb\u53bb\uff0c\u5e76\u4e14\u6307\u5b9a\u5faa\u73af\u6b21\u6570\uff0c\u6bcf\u6b21\u5faa\u73af\u90fd\u628a\u6570\u7ec4\u540e\u9762\u7684\u6210\u5458\u653e\u524d\u9762\u3002\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">(function (myArr, num){\n    var xiaojianbang = function (nums){\n        while(--nums){\n            myArr.unshift(myArr.pop());\n        }\n    };\n    xiaojianbang(++num);\n}(bigArr\uff0c 0x10));<\/code><\/pre>\n<p>\u65e2\u7136\u6570\u7ec4\u7684\u987a\u5e8f\u548c\u539f\u5148\u4e0d\u4e00\u6837\u4e86\uff0c\u90a3\u4e48\u88ab\u6df7\u6dc6\u7684\u4ee3\u7801\u5728\u6267\u884c\u4e4b\u524d\uff0c\u80af\u5b9a\u8fd8\u662f\u8981\u8fd8\u539f\u7684\u3002\u56e0\u6b64\u8fd8\u9700\u8981\u4e00\u6bb5\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\u3002\u4ee3\u7801\u4e5f\u5f88\u7b80\u5355\uff0c\u53cd\u7740\u6765\u5373\u53ef\uff0c\u5faa\u73af\u540c\u6837\u7684\u6b21\u6570\uff0c\u628a\u6570\u7ec4\u524d\u9762\u7684\u6210\u5458\u653e\u540e\u9762\u3002\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">(function (myArr, num) {\n    var xiaojianbang = function (nums) {\n        while (--nums) {\n            myArr.push(myArr.shift ());\n        }\n    };\n    xiaojianbang(++num);\n})(arr, 0x10);<\/code><\/pre>\n<p>\u8fd8\u539f\u987a\u5e8f\u7684\u4ee3\u7801\u80af\u5b9a\u662f\u8981\u548c\u88ab\u6df7\u6dc6\u7684\u4ee3\u7801\u653e\u4e00\u8d77\u7684\u3002\u5982\u4f55\u653e\u4e00\u8d77\u5462? \u628a\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\u4fdd\u5b58\u5230\u65b0\u6587\u4ef6 demoFront.js\uff0c\u8bfb\u53d6\u8be5\u6587\u4ef6\u5e76\u89e3\u6790\u6210 astFront\u3002\u7531\u4e8e\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\u6700\u5916\u5c42\u53ea\u6709\u4e00\u4e2a\u51fd\u6570\u8282\u70b9\uff0c\u6240\u4ee5\u53d6\u51fa\u5176\u4e2d\u7684astFront.program.body[0]\u653e\u5165\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u4e2d\u7684body\u4e2d\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/\u6784\u5efa\u6570\u7ec4\u58f0\u660e\u8bed\u53e5\nbigArr = t.variableDeclarator(t.identifier(&#039;arr&#039;), t.arrayExpression(bigArr));\nbigArr = t.variableDeclaration(&#039;var&#039;, [bigArr]);\n\n\/\/\u8bfb\u53d6\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u51fd\u6570\uff0c\u5e76\u89e3\u6790\u6210astFront\nconst jscodeFront = fs.readFileSync(&quot;.\/demoFront.js&quot;, {\n    encoding: &quot;utf-8&quot;\n  });\nlet astFront = parser.parse(jscodeFront);\n\/\/\u5148\u628a\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\uff0c\u52a0\u5165\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u7684ast\u4e2d\nast.program.body.unshift(astFront.program.body[0]);\n\/\/\u628a\u6570\u7ec4\u653e\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u7684ast\u6700\u524d\u9762\nast.program.body.unshift(bigArr);<\/code><\/pre>\n<p>\u4ece\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u6570\u7ec4\u6df7\u6dc6\u4e2d\u751f\u6210\u7684\u6570\u7ec4\uff0c\u9700\u8981\u653e\u5230\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\u524d\u9762\u3002<\/p>\n<p>\u5168\u90e8\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\nfunction base64Encode(e) {\n    var r, a, c, h, o, t, base64EncodeChars = &#039;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/&#039;;\n    for (c = e.length, a = 0, r = &#039;&#039;; a &lt; c;) {\n        if (h = 255 &amp; e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4),\n            r += &#039;==&#039;;\n            break\n        }\n        if (o = e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n            r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2),\n            r += &#039;=&#039;;\n            break\n        }\n        t = e.charCodeAt(a++),\n        r += base64EncodeChars.charAt(h &gt;&gt; 2),\n        r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n        r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2 | (192 &amp; t) &gt;&gt; 6),\n        r += base64EncodeChars.charAt(63 &amp; t)\n    }\n    return r\n}\n\nlet bigArr = [];\ntraverse(ast, {\n    StringLiteral(path){\n        let cipherText = base64Encode(path.node.value);\n        let bigArrIndex = bigArr.indexOf(cipherText);\n        let index = bigArrIndex;\n        if(bigArrIndex == -1){\n            let length = bigArr.push(cipherText);\n            index = length -1;\n        }\n        let encStr = t.callExpression(\n                    t.identifier(&#039;atob&#039;), \n                    [t.memberExpression(t.identifier(&#039;arr&#039;), \n                                     t.numericLiteral(index), true)]);\n        path.replaceWith(encStr);\n    }\n});\nbigArr = bigArr.map(function(v){\n  return t.stringLiteral(v);\n});\n\/\/\u6784\u5efa\u6570\u7ec4\u58f0\u660e\u8bed\u53e5\nbigArr = t.variableDeclarator(t.identifier(&#039;arr&#039;), t.arrayExpression(bigArr));\nbigArr = t.variableDeclaration(&#039;var&#039;, [bigArr]);\n\n\/\/\u8bfb\u53d6\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u51fd\u6570\uff0c\u5e76\u89e3\u6790\u6210astFront\nconst jscodeFront = fs.readFileSync(&quot;.\/demoFront.js&quot;, {\n    encoding: &quot;utf-8&quot;\n  });\nlet astFront = parser.parse(jscodeFront);\n\/\/\u5148\u628a\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\uff0c\u52a0\u5165\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u7684ast\u4e2d\nast.program.body.unshift(astFront.program.body[0]);\n\/\/\u628a\u6570\u7ec4\u653e\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u7684ast\u6700\u524d\u9762\nast.program.body.unshift(bigArr);\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\n    var arr = [&quot;5Q==&quot;, &quot;AA==&quot;, &quot;jA==&quot;, &quot;CQ==&quot;, &quot;2w==&quot;, &quot;lA==&quot;, &quot;bQ==&quot;, &quot;MA==&quot;, &quot;eXl5eS1NTS1kZA==&quot;];\n    (function (myArr, num) {\n      var xiaojianbang = function (nums) {\n        while (--nums) {\n          myArr.push(myArr.shift());\n        }\n      };\n      xiaojianbang(++num);\n    })(arr, 0x10);\n    Date.prototype.format = function (formatStr) {\n      var str = formatStr;\n      var Week = [atob(arr[0]), atob(arr[1]), atob(arr[2]), atob(arr[3]), atob(arr[4]), atob(arr[5]), atob(arr[6])];\n      str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n      str = str.replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : atob(arr[7]) + (this.getMonth() + 1));\n      str = str.replace(\/dd|DD\/, this.getDate() &gt; 9 ? this.getDate().toString() : atob(arr[7]) + this.getDate());\n      return str;\n    };\n    console.log(new Date().format(atob(arr[8])));\n*\/<\/code><\/pre>\n<h3>5.\u5b9e\u73b0\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/h3>\n<p>\u5b9e\u73b0\u6570\u7ec4\u987a\u5e8f\u8fd8\u539f\u7684\u4ee3\u7801\u662f\u6ca1\u6709\u7ecf\u8fc7\u6df7\u6dc6\u7684\uff0c\u4ee3\u7801\u4e2d\u6807\u8bc6\u7b26\u7684\u6df7\u6dc6\uff0c\u53ef\u4ee5\u5728\u6700\u540e\u4e00\u8d77\u5904\u7406\u3002<\/p>\n<p>\u5176\u4e2d\u50cf push\u3001shift \u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff0c\u7531\u4e8e\u8fd9\u4e9b\u4ee3\u7801\u5904\u4e8e\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\u4e2d\uff0c\u56e0\u6b64\u6ca1\u6cd5\u628a\u5b83\u4eec\u63d0\u53d6\u5230\u5927\u6570\u7ec4\u4e2d\u3002\u7b14\u8005\u5728\u8fd9\u91cc\u5c31\u7b80\u5355\u5730\u628a\u5b83\u4eec\u7f16\u7801\u6210\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demoFront.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\nfunction hexEnc(code) {\n    for (var hexStr = [], i = 0, s; i &lt; code.length; i++) {\n        s = code.charCodeAt(i).toString(16);\n        hexStr += &quot;\\\\x&quot; + s;\n    }\n    return hexStr\n}\ntraverse(ast,{\n    MemberExpression(path){\n        if(t.isIdentifier(path.node.property)){\n            let name = path.node.property.name;\n            path.node.property = t.stringLiteral(hexEnc(name));\n        }\n        path.node.computed = true;\n    }\n});\nlet code = generator(ast).code;\nconsole.log(code);\n\/*\n    (function (myArr, num) {\n      var xiaojianbang = function (nums) {\n        while (--nums) {\n          myArr[&quot;\\\\x70\\\\x75\\\\x73\\\\x68&quot;](myArr[&quot;\\\\x73\\\\x68\\\\x69\\\\x66\\\\x74&quot;]());\n        }\n      };\n      xiaojianbang(++num);\n    })(arr, 0x10);\n*\/<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u904d\u5386 MemberExpression \u8282\u70b9\uff0c\u5c06property\u7684\u503c\u8fdb\u884c hex \u7f16\u7801\uff0c\u7136\u540e\u7528stringLiteral\u8282\u70b9\u66ff\u6362 property,\u5e76\u628a computed \u6539\u4e3a true \u6700\u540e\u8f93\u51fa\u7684\u7ed3\u679c\u6709\u4e24\u4e2a\u8f6c\u4e49\u5b57\u7b26\uff0c\u5176\u5b9e\u662f Babel \u5728\u5904\u7406\u7684\u8282\u70b9\u7684\u65f6\u5019\uff0c\u81ea\u52a8\u8f6c\u4e49\u4e86\u53cd\u659c\u6760\u3002\u5728\u6700\u540e\u8f6c\u6210\u4ee3\u7801\u4ee5\u540e\uff0c\u628a\u5b83\u4eec\u66ff\u6362\u6389\u5373\u53ef<\/p>\n<pre><code class=\"language-javascript\">let code = generator(ast).code;\ncode = code.replace(\/\\\\\\\\x\/g, &#039;\\\\x&#039;);\nconsole.log(code);<\/code><\/pre>\n<p>\u53e6\u5916\uff0cunicode\u5b57\u7b26\u7533\u7684\u5b9e\u73b0\u65b9\u5f0f\u4e0e\u672c\u5c0f\u8282\u4e00\u81f4\uff0c\u628ahex\u7f16\u7801\u7684\u51fd\u6570\u6362\u6210unicode \u7f16\u7801\u7684\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<h3>6.\u5b9e\u73b0\u6807\u8bc6\u7b26\u6df7\u6dc6<\/h3>\n<p>\u7a0b\u5e8f\u5458\u5728\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u6807\u8bc6\u7b26\u4e00\u822c\u90fd\u662f\u6709\u8bed\u4e49\u7684\uff0c\u6839\u636e\u6807\u8bc6\u7b26\u540d\u5c31\u53ef\u4ee5\u731c\u6d4b\u51fa\u4ee3\u7801\u5728\u505a\u4ec0\u4e48\uff0c\u56e0\u6b64\u6807\u8bc6\u7b26\u6df7\u6dc6\u5f88\u6709\u5fc5\u8981\u3002<\/p>\n<p>\u6709\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u6807\u8bc6\u7b26\u6df7\u6dc6\u65b9\u6848\uff0c\u904d\u5386binding.scope.block\uff0c\u627e\u5230\u76f8\u5173\u8868\u8fbe\u5f0f\u7684\u503c\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6848\u7684\u7f3a\u70b9\u662f\u6574\u4e2a\u6587\u4ef6\u4e2d\u4e0d\u540c\u6807\u8bc6\u7b26\u7684\u540d\u79f0\u90fd\u662f\u4e0d\u4e00\u6837\u7684\u3002\u4f46\u5b9e\u9645\u4e0a\u53ef\u4ee5\u8ba9\u5404\u4e2a\u51fd\u6570\u4e4b\u95f4\u7684\u5c40\u90e8\u6807\u8bc6\u7b26\u540d\u76f8\u540c\uff0c\u51fd\u6570\u5185\u7684\u5c40\u90e8\u6807\u8bc6\u7b26\u540d\u8fd8\u53ef\u4ee5\u4e0e\u6ca1\u6709\u5f15\u7528\u5230\u7684\u5168\u5c40\u6807\u8bc6\u7b26\u540d\u76f8\u540c\u3002\u8fd9\u6837\u505a\u66f4\u5177\u8ff7\u60d1\u6027\u3002\u8fd9\u4e00\u5c0f\u8282\u5c31\u6765\u5b9e\u73b0\u4e00\u4e0b\u8fd9\u79cd\u65b9\u6848\u3002<\/p>\n<p>\u8981\u5b9e\u73b0\u8fd9\u79cd\u65b9\u6848\uff0c\u9700\u8981\u7528\u5230\u4e00\u4e2a\u65b9\u6cd5 scope.getOwnBinding\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5c5e\u4e8e\u5f53\u524d\u8282\u70b9\u7684\u81ea\u5df1\u7684\u7ed1\u5b9a\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u5728 Program \u8282\u70b9\u4e0b\uff0c\u4f7f\u7528 getOwnBinding \u5c31\u53ef\u4ee5\u83b7\u53d6\u5230\u5168\u5c40\u6807\u8bc6\u7b26\u540d\uff0c\u800c\u51fd\u6570\u5185\u5c40\u90e8\u6807\u8bc6\u7b26\u540d\u4e0d\u4f1a\u7834\u83b7\u53d6\u5230\u3002<\/p>\n<p>\u90a3\u4e48\u8981\u83b7\u53d6\u5230\u5c40\u90e8\u6807\u8bc6\u7b26\u540d\uff0c\u53ef\u4ee5\u904d\u5386\u51fd\u6570\u8282\u70b9\uff0c\u5728FunctionExpression\u4e0eFunctionDeclaration \u8282\u70b9\u4e0b\uff0c\u4f7f\u7528gotOwmBinding\u4f1a\u83b7\u53d6\u5230\u51fd\u6570\u81ea\u8eab\u5b9a\u4e49\u7684\u5c40\u90e8\u6807\u8bc6\u7b26\u540d\uff0c\u800c\u4e0d\u4f1a\u83b7\u53d6\u5230\u5168\u5c40\u6807\u8bc6\u7b26\u540d\u3002<\/p>\n<p>\u904d\u5386\u4e09\u79cd\u8282\u70b9\uff0c\u6267\u884c\u540c\u4e00\u4e2a\u91cd\u547d\u540d\u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">traverse(ast, {\n    &#039;Program|FunctionExpression|FunctionDeclaration&#039;(path) {\n        renameOwnBinding(path);\n    }\n});<\/code><\/pre>\n<p>\u90a3\u4e48renameOwnBinding\u51fd\u6570\u5982\u4f55\u5b9e\u73b0\u5462?\u6765\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801:<\/p>\n<pre><code class=\"language-javascript\">function renameOwnBinding(path) {\n    let OwnBindingObj = {}, globalBindingObj = {}, i = 0;\n    path.traverse({\n        Identifier(p)\u00a0 {\n            let name = p.node.name;\n            let binding = p.scope.getOwnBinding(name);\n            binding ? (OwnBindingObj[name] = binding) : (globalBindingObj[name] = 1);\n        }\n    });\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5148\u904d\u5386\u5f53\u524d\u8282\u70b9\u4e2d\u6240\u6709\u7684 Identifier\uff0c\u5f97\u5230Identifier \u7684 name \u5c5e\u6027\uff0c\u901a\u8fc7getOwnBinding\u5224\u65ad\u662f\u5426\u5f53\u524d\u8282\u70b9\u81ea\u5df1\u7684\u7ed1\u5b9a\u3002<\/p>\n<p>\u5982\u679c binding \u4e3aundefined\uff0c\u5219\u88c5\u793a\u662f\u5176\u7236\u7ea7\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u6216\u8005\u5168\u5c40\u7684\u6807\u8bc6\u7b26,\u5c31\u5c06\u8be5\u6807\u8bc6\u7b26\u540d\u4f5c\u4e3a\u5c5e\u6027\u540d,\u653e\u5165\u5230 globalBindingObj\u5bf9\u8c61\u4e2d\u3002<\/p>\n<p>\u5982\u679c binding \u5b58\u5728\uff0c\u5219\u8868\u793a\u662f\u5f53\u524d\u8282\u70b9\u81ea\u5df1\u7684\u7ed1\u5b9a\uff0c\u5c31\u5c06\u8be5\u6807\u8bc6\u7b26\u4f5c\u4e3a\u5c5e\u6027\u540d\uff0cbinding \u4f5c\u4e3a\u5c5e\u6027\u503c\uff0c\u653e\u5165\u5230 OwnBindingObj \u5bf9\u8c61\u4e2d\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u56db\u70b9\u9700\u8981\u6ce8\u610f\u4e00\u4e0b:<\/p>\n<ol>\n<li>globalBindingObj \u4e2d\u5b58\u653e\u7684\u4e0d\u662f\u6240\u6709\u7684\u5168\u5c40\u6807\u8bc6\u7b26\uff0c\u800c\u662f\u5f53\u524d\u8282\u70b9\u5f15\u7528\u5230\u7684\u5168\u5c40\u6807\u8bc6\u7b26\u3002\u56e0\u4e3a\u91cd\u547d\u540d\u6807\u8bc6\u7b26\u7684\u65f6\u5019\uff0c\u4e0d\u80fd\u4e0e\u5f15\u7528\u5230\u7684\u5168\u5c40\u6807\u8bc6\u7b26\u91cd\u540d\uff0c\u9700\u8981\u8fdb\u884c\u5224\u65ad\u3002\u81f3\u4e8e\u6ca1\u6709\u5f15\u7528\u5230\u7684\u5168\u5c40\u6807\u8bc6\u7b26\u540d\uff0c\u5c31\u662f\u8981\u91cd\u540d\u624d\u66f4\u5177\u8ff7\u60d1\u6027\uff0c\u53cd\u6b63\u6700\u540e\u4f7f\u7528\u7684\u8fd8\u662f\u5f53\u524d\u8282\u70b9\u5b9a\u4e49\u7684\u5c40\u90e8\u6807\u8bc6\u7b26\u3002<\/li>\n<li>OwnBindingObj\u4e2d\u5b58\u50a8\u5bf9\u5e94\u6807\u8bc6\u7b26\u7684 binding\u3002\u56e0\u4e3a\u91cd\u547d\u540d\u6807\u8bc6\u7b26\u7684\u65f6\u5019\uff0c\u9700\u8981\u4f7f\u7528 binding.scope.rename \u65b9\u6cd5\u3002<\/li>\n<li>\u628a\u6807\u8bc6\u7b26\u540d\u4f5c\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u3002\u56e0\u4e3a\u4e00\u4e2a Identifier \u6709\u591a\u5904\u5f15\u7528\u5c31\u4f1a\u904d\u5386\u5230\u591a\u4e2a\uff0c\u4f46\u5b9e\u9645\u4e0a\u53ea\u9700\u8981\u8c03\u7528\u4e00\u6b21 scope.rename \u5373\u53ef\u5b8c\u6210\u6240\u6709\u5f15\u7528\u5904\u7684\u91cd\u547d\u540d\u3002\u800c\u5bf9\u8c61\u5c5e\u6027\u540d\u5177\u6709\u552f\u4e00\u6027\uff0c\u5c31\u53ef\u4ee5\u53ea\u4fdd\u7559\u6700\u540e\u4e00\u4e2a\u540c\u540d\u6807\u8bc6\u7b26\u3002<\/li>\n<li>\u6700\u597d\u628a ast \u5148\u8f6c\u6210\u4ee3\u7801\uff0c\u518d\u6b21\u8fdb\u884c\u89e3\u6790\u540e\u518d\u8fdb\u884c\u6807\u8bc6\u7b26\u6df7\u6dc6\u3002\u4e4b\u524d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\u4fee\u6539AST \u8282\u70b9\u7684\u65f6\u5019\uff0c\u4f7f\u7528 Path \u5bf9\u8c61\u7684\u65b9\u6cd5\u7684\u8bdd\uff0cBabel \u4f1a\u66f4\u65b0 Path \u4fe1\u606f\u3002\u4f46\u662f\u5b9e\u9645\u5e94\u7528\u4e2d\u5e76\u4e0d\u80fd\u505a\u5230\u5168\u90e8\u4f7f\u7528 Path \u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u7528 types \u7ec4\u4ef6\u751f\u6210\u65b0\u8282\u70b9\u7531\u4e8e types \u7ec4\u4ef6\u751f\u6210\u7684\u662f\u8282\u70b9\uff0c\u5e76\u4e0d\u662f Path \u5bf9\u8c61\uff0c\u90a3\u4e48 binding \u4ece\u4f55\u800c\u6765?<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u53bb\u80af\u5b9a\u662f\u8981\u904d\u5386 OwnBindingObj \u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\uff0c\u6765\u8fdb\u884c\u91cd\u547d\u540d\u4e86\uff0c\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">for(let oldName in OwnBindingObj) {\n    do {\n        var newName = &#039;_0x2ba6ea&#039; + i++;\n    } while(globalBindingObj[newName]);\n    OwnBindingObj[oldName].scope.rename(oldName, newName);\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u4f7f\u7528 do...while \u5faa\u73af\u6765\u968f\u673a\u53d6\u4e00\u4e2a\u6807\u8bc6\u7b26\u540d\uff0c\u76f4\u5230\u4e0e\u5f53\u524d\u8282\u70b9\u5f15\u7528\u5230\u7684\u5168\u5c40\u6807\u8bc6\u7b26\u540d\u4e0d\u4e00\u6837\u7684\u65f6\u5019\uff0c\u8fdb\u884c\u91cd\u547d\u540d\u3002\u4e0a\u8ff0\u4ee3\u7801\u770b\u4f3c\u5b8c\u7f8e\uff0c\u4f46\u5b9e\u9645\u4e0a getOmnBinding \u4f1a\u53d6\u5230\u5f53\u524d\u51fd\u6570\u4e2d\u7684\u5b50\u51fd\u6570\u7684\u53c2\u6570\u540d\u3002\u867d\u7136\u4e0d\u5f71\u54cd\u6700\u540e\u7ed3\u679c\uff0c\u56e0\u4e3a\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u5bf9\u5b50\u51fd\u6570\u7684\u53c2\u6570\u8fdb\u884c\u91cd\u547d\u540d\u4e86\uff0c\u5f53\u904d\u5386\u5230\u5b50\u51fd\u6570\u7684\u65f6\u5019\uff0c\u8fd8\u4f1a\u518d\u6b21\u91cd\u547d\u540d\u3002\u7b14\u8005\u5728\u8fd9\u91cc\u4f7f\u7528\u6bd4\u8f83\u6807\u8bc6\u7b26\u4f5c\u7528\u57df\u7684\u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u5b8c\u6574\u7684\u6df7\u6dc6\u6807\u8bc6\u7b26\u7684\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\nfunction renameOwnBinding(path) {\n    let OwnBindingObj = {}, globalBindingObj = {}, i = 0;\n    path.traverse({\n        Identifier(p)\u00a0 {\n            let name = p.node.name;\n            let binding = p.scope.getOwnBinding(name);\n            binding &amp;&amp; generator(binding.scope.block).code == path + &#039;&#039; ?\n            (OwnBindingObj[name] = binding) : (globalBindingObj[name] = 1);\n        }\n    });\n    for(let oldName in OwnBindingObj) {\n        do {\n            var newName = &#039;_0x2ba6ea&#039; + i++;\n        } while(globalBindingObj[newName]);\n        OwnBindingObj[oldName].scope.rename(oldName, newName);\n    }\n}\ntraverse(ast, {\n    &#039;Program|FunctionExpression|FunctionDeclaration&#039;(path) {\n        renameOwnBinding(path);\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});<\/code><\/pre>\n<p>\u5355\u72ec\u89e3\u91ca\u4e0b<code>binding  &amp;&amp; generator(binding.scope.block).code == path + &#039;&#039;<\/code> \uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e2d binding.scope,block \u8868\u793a binding \u7684\u4f5c\u7528\u57df\uff0cpath + ''\u53ea\u8868\u793a\u628a\u5f53\u524d\u8282\u70b9\u8f6c\u4ee3\u7801\u3002\u6ce8\u610f\uff0c\u8fd9\u91ccpath \u6307\u7684\u662fProgram|FunctionExpression|FunctionDeclaration\u5176\u4e2d\u4e00\u4e2a\u3002\u5b50\u51fd\u6570\u7684\u53c2\u6570\u4f5c\u7528\u57df\u8f6c\u4ee3\u7801\u540e\u662f\u5b50\u51fd\u6570\u672c\u8eab\uff0c\u80af\u5b9a\u4e0e\u5f53\u524d\u8282\u70b9\u8f6c\u4ee3\u7801\u540e\u4e0d\u4e00\u81f4\u3002<\/p>\n<h3>7. \u6807\u8bc6\u7b26\u7684\u968f\u673a\u751f\u6210<\/h3>\n<p>\u5728\u521a\u521a\u7684\u5c0f\u8282\u4e2d\uff0c\u91cd\u547d\u540d\u6807\u8bc6\u7b26\u65f6\u4f7f\u7528\u56fa\u5b9a\u7684_0x2aea \u52a0\u4e0a\u4e00\u4e2a\u81ea\u589e\u7684\u6570\u5b57\u6765\u4f5c\u4e3a\u65b0\u7684\u6807\u8bc6\u7b26\u540d\u3002\u8fd9\u4e00\u8282\u5c06\u4f7f\u7528\u5927\u5199\u5b57\u6bcd O\u5c0f\u5199\u5b57\u6bcdo\u548c\u6570\u5b570\u8fd9\u4e09\u4e2a\u5b57\u7b26\u6765\u7ec4\u6210\u6807\u8bc6\u7b26\u540d\uff0c\u628a<code>var newName=&#039;_0x2ba6ea&#039; + i++<\/code>;\u8fd9\u53e5\u4ee3\u7801\u6539\u6210<code>var newName=generatorIdentifier(i++);<\/code>,\u4ee5\u4e0b\u662f generatorIdentifier \u7684\u5b9e\u73b0\u4ee3\u7801:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\nfunction generatorIdentifier(decNum){\n    let flag = [&#039;O&#039;, &#039;o&#039;, &#039;0&#039;];\n    let retval = [];\n    while(decNum &gt; 0){\n        retval.push(decNum % 3);\n        decNum = parseInt(decNum \/ 3);\n    }\n    let Identifier = retval.reverse().map(function(v){\n        return flag[v]\n    }).join(&#039;&#039;);\n    Identifier.length &lt; 6 ? (Identifier = (&#039;OOOOOO&#039; + Identifier).substr(-6)):\n    Identifier[0] == &#039;0&#039; &amp;&amp; (Identifier = &#039;O&#039; + Identifier);\n    return Identifier;\n}\n\nfunction renameOwnBinding(path) {\n    let OwnBindingObj = {}, globalBindingObj = {}, i = 0;\n    path.traverse({\n        Identifier(p)\u00a0 {\n            let name = p.node.name;\n            let binding = p.scope.getOwnBinding(name);\n            binding &amp;&amp; generator(binding.scope.block).code == path + &#039;&#039; ?\n            (OwnBindingObj[name] = binding) : (globalBindingObj[name] = 1);\n        }\n    });\n    for(let oldName in OwnBindingObj) {\n        do {\n            var newName = generatorIdentifier(i++);\n        } while(globalBindingObj[newName]);\n        OwnBindingObj[oldName].scope.rename(oldName, newName);\n    }\n}\ntraverse(ast, {\n    &#039;Program|FunctionExpression|FunctionDeclaration&#039;(path) {\n        renameOwnBinding(path);\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\n    Date.prototype.format = function (OOOOOO) {\n      var OOOOOo = OOOOOO;\n      var OOOOO0 = [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;];\n      OOOOOo = OOOOOo.replace(\/yyyy|YYYY\/, this.getFullYear());\n      OOOOOo = OOOOOo.replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : &#039;0&#039; + (this.getMonth() + 1));\n      OOOOOo = OOOOOo.replace(\/dd|DD\/, this.getDate() &gt; 9 ? this.getDate().toString() : &#039;0&#039; + this.getDate());\n      return OOOOOo;\n    };\n    console.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n*\/<\/code><\/pre>\n<p>\u5b83\u672c\u8d28\u4e0a\u5c31\u662f\u5c06\u5341\u8fdb\u5236\u8f6c\u4e3a\u4e09\u8fdb\u5236,\u7136\u540e\u628a 0\u30011\u30012 \u5206\u522b\u7528\u5927\u5199\u5b57\u6bcdO \u3001\u5c0f\u5199\u5b57\u6bcdo\u3001\u548c\u6570\u5b570\u6765\u6362\u3002<\/p>\n<p>retval \u6570\u7ec4\u4e2d\u5b58\u7684\u662f\u4f59\u6570\uff0c\u7ecf\u8fc7\u6570\u7ec4\u7684 reverse \u65b9\u6cd5\u5012\u5e8f\u540e,\u5c31\u5f97\u5230\u5341\u8fdb\u5236\u4e3a\u4e09\u8fdb\u5236\u7684\u7ed3\u679c,\u53ea\u662f\u8fd9\u65f6\u91cc\u9762\u7684\u6570\u5b57\u662f 012\u3002\u63a5\u7740\u901a\u8fc7\u6570\u7ec4\u7684 map \u904d\u5386\u6570\u7ec4\u6210\u5458012,\u5206\u522b\u66ff\u6362\u6210\u5927\u5199\u5b57\u6bcd O,\u5c0f\u5199\u5b57\u6bcdo\u548c\u6570\u5b57 0\u3002\u6700\u540e\u7528\u6570\u7ec4\u7684 join \u628a\u6570\u7ec4\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4e3a\u4e86\u4f7f\u751f\u6210\u7684\u6807\u8bc6\u7b26\u6574\u9f50,\u5bf9\u4e8e\u957f\u5ea6\u5c0f\u4e8e 6\u7684\u6807\u8bc6\u7b26,\u53ef\u4ee5\u7528\u5927\u5199\u5b57\u6bcdO \u6216\u8005\u5c0f\u5199\u5b57\u6bcdo\u8865\u5168\u4f4d\u6570\uff1b\u5bf9\u4e8e\u957f\u5ea6\u5927\u4e8e\u6216\u7b49\u4e8e 6\u5e76\u4e14\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u6570\u5b570\u7684\u6807\u8bc6\u7b26,\u5219\u5f80\u524d\u9762\u8865\u4e00\u4e2a\u5927\u5b57\u6bcdO\u6216\u8005\u5c0f\u5199\u5b57\u6bcdo(\u6807\u8bc6\u7b26\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934)\u3002<\/p>\n<h2>\u4e09\u3001\u4ee3\u7801\u5757\u7684\u6df7\u6dc6<\/h2>\n<h3>1.\u4e8c\u9879\u5f0f\u8f6c\u51fd\u6570\u82b1\u6307\u4ee4<\/h3>\n<p>\u82b1\u6307\u4ee4\u7528\u6765\u5c3d\u53ef\u80fd\u5730\u9690\u85cf\u539f\u4ee3\u7801\u7684\u771f\u5b9e\u610f\u56fe,\u5e76\u4e14\u6709\u591a\u79cd\u5b9e\u73b0\u65b9\u6848\u3002\u672c\u8282\u8981\u4ecb\u7ecd\u5982\u4f55\u628a\u4e8c\u9879\u5f0f\u8f6c\u4e3a\u51fd\u6570\u3002<\/p>\n<p>\u4f8b\u5982,\u628a c+d \u8f6c\u6362\u4e3a\u5982\u4e0b\u5f62\u5f0f:<\/p>\n<pre><code class=\"language-javascript\">function xxx(a,b){\n    return a + b;\n}\nxxx(c,d);<\/code><\/pre>\n<p>\u4e0d\u6b62\u4e8c\u9879\u5f0f,\u4ee3\u7801\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u5904\u7406\u6210\u7c7b\u4f3c\u7684\u82b1\u6307\u4ee4,\u4f8b\u5982 c(d)\u53ef\u4ee5\u8f6c\u6362\u6210\u5982\u4e0b\u5f62\u5f0f:<\/p>\n<pre><code class=\"language-javascript\">function xxx(a,b) {\n    return a(b);\n}\nxxx(c\uff0cd);<\/code><\/pre>\n<p>\u5728\u8fd9\u91cc\u53ea\u5b9e\u73b0\u4e8c\u9879\u5f0f\u7684\u60c5\u51b5\u3002\u4ece AST \u7684\u89d2\u5ea6\u53bb\u8003\u8651\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e2a\u65b9\u6848,\u5206\u4e3a\u4ee5\u4e0b\u56db\u6b65:<\/p>\n<ol>\n<li>\u904d\u5386 BinaryExpression \u8282\u70b9,\u53d6\u51faoperator\u3001left \u548c right\u3002<\/li>\n<li>\u751f\u6210\u4e00\u4e2a\u51fd\u6570\uff0c\u51fd\u6570\u540d\u4e0d\u80fd\u4e0e\u5f53\u524d\u8282\u70b9\u4e2d\u7684\u6807\u8bc6\u7b26\u51b2\u7a81,\u53c2\u6570\u53ef\u4ee5\u56fa\u5b9a\u4e3aa\u548cb,\u8fd4\u56de\u8bed\u53e5\u4e2d\u7684\u8fd0\u7b97\u7b26\u4e0e operator \u4e00\u81f4\u3002<\/li>\n<li>\u627e\u5230\u6700\u8fd1\u7684 BlockStatement \u8282\u70b9,\u5c06\u751f\u6210\u7684\u51fd\u6570\u52a0\u5165\u5230 body \u6570\u7ec4\u4e2d\u7684\u6700\u524d\u9762\u3002<\/li>\n<li>\u628a\u539f\u5148\u7684 BinaryExpression \u8282\u70b9\u66ff\u6362\u4e3a CallExpression,callee \u662f\u51fd\u6570\u540d\uff0c_arguments \u662f\u4e00\u9879\u5f0f\u7684 let \u548cright\u3002<\/li>\n<\/ol>\n<p>\u4e0a\u8ff0\u5b9e\u73b0\u65b9\u6848\u4e2d,\u6807\u8bc6\u7b26\u540d\u53ef\u4ee5\u968f\u673a\u8bbe\u7f6e,\u56e0\u4e3a\u6700\u540e\u8fdb\u884c\u6807\u8bc6\u7b26\u6df7\u6dc6\u65f6,\u4f1a\u5904\u7406\u6210\u76f8\u4f3c\u7684\u540d\u5b57\u3002\u82b1\u6307\u4ee4\u7684\u76ee\u7684\u662f\u589e\u52a0\u4ee3\u7801\u91cf,\u9690\u85cf\u4ee3\u7801\u7684\u771f\u5b9e\u610f\u56fe\u3002\u56e0\u6b64\u6bcf\u904d\u5386\u5230\u4e00\u4e2a operator\uff0c\u90fd\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u540d\u5b57\u7684\u51fd\u6570,\u4e0d\u9700\u8981\u53bb\u5224\u65ad\u662f\u54ea\u79cdoperator,\u56e0\u4e3a\u5e76\u4e0d\u662f\u4e00\u79cdoperator\u751f\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u5b9e\u73b0\u7684\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    BinaryExpression(path){\n        let operator = path.node.operator;\n        let left = path.node.left;\n        let right = path.node.right;\n        let a = t.identifier(&#039;a&#039;);\n        let b = t.identifier(&#039;b&#039;);\n        let funcNameIdentifier = path.scope.generateUidIdentifier(&#039;xxx&#039;);\n        let func = t.functionDeclaration(\n            funcNameIdentifier, \n            [a, b], \n            t.blockStatement([t.returnStatement(\n                    t.binaryExpression(operator, a, b)\n                )]));\n        let BlockStatement = path.findParent(\n                    function(p){return p.isBlockStatement()});\n        BlockStatement.node.body.unshift(func);\n        path.replaceWith(t.callExpression(funcNameIdentifier, [left, right]));\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\n    Date.prototype.format = function (formatStr) {\n      function _xxx7(a, b) {\n        return a + b;\n      }\n      function _xxx6(a, b) {\n        return a &gt; b;\n      }\n      function _xxx5(a, b) {\n        return a + b;\n      }\n      function _xxx4(a, b) {\n        return a + b;\n      }\n      function _xxx3(a, b) {\n        return a + b;\n      }\n      function _xxx2(a, b) {\n        return a + b;\n      }\n      function _xxx(a, b) {\n        return a &gt; b;\n      }\n      var str = formatStr;\n      var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;];\n      str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n      str = str.replace(\/MM\/, _xxx(_xxx2(this.getMonth(), 1), 9) ? _xxx3(this.getMonth(), 1).toString() : _xxx4(&#039;0&#039;, _xxx5(this.getMonth(), 1)));\n      str = str.replace(\/dd|DD\/, _xxx6(this.getDate(), 9) ? this.getDate().toString() : _xxx7(&#039;0&#039;, this.getDate()));\n      return str;\n    };\n    console.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n*\/<\/code><\/pre>\n<h3>2.\u4ee3\u7801\u7684\u9010\u884c\u52a0\u5bc6<\/h3>\n<p>\u8fd9\u79cd\u65b9\u6848\u7684\u539f\u7406\u8ddf\u5b57\u7b26\u4e32\u52a0\u5bc6\u662f\u4e00\u6837\u7684,\u4e0d\u8fc7\u9700\u8981\u5148\u628a\u4ee3\u7801\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32,\u518d\u628a\u5b57\u7b26\u7533\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\u4f20\u5165\u89e3\u5bc6\u51fd\u6570\u3001\u89e3\u5bc6\u51fa\u660e\u6587,\u7136\u540e\u901a\u8fc7 eval \u6267\u884c\u4ee3\u7801\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\nfunction base64Encode(e) {\n    var r, a, c, h, o, t, base64EncodeChars = &#039;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/&#039;;\n    for (c = e.length, a = 0, r = &#039;&#039;; a &lt; c;) {\n        if (h = 255 &amp; e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4),\n            r += &#039;==&#039;;\n            break\n        }\n        if (o = e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n            r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2),\n            r += &#039;=&#039;;\n            break\n        }\n        t = e.charCodeAt(a++),\n        r += base64EncodeChars.charAt(h &gt;&gt; 2),\n        r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n        r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2 | (192 &amp; t) &gt;&gt; 6),\n        r += base64EncodeChars.charAt(63 &amp; t)\n    }\n    return r\n}\ntraverse(ast, {\n    FunctionExpression(path){\n        let blockStatement = path.node.body;\n        let Statements = blockStatement.body.map(function(v){\n            if(t.isReturnStatement(v)) return v;\n            let code = generator(v).code;\n            let cipherText = base64Encode(code);\n            let decryptFunc = t.callExpression(t.identifier(&#039;atob&#039;), [t.stringLiteral(cipherText)]);\n            return t.expressionStatement(t.callExpression(t.identifier(&#039;eval&#039;), [decryptFunc]));\n        });\n        path.get(&#039;body&#039;).replaceWith(t.blockStatement(Statements));\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\n    Date.prototype.format = function (formatStr) {\n      eval(atob(&quot;dmFyIHN0ciA9IGZvcm1hdFN0cjs=&quot;));\n      eval(atob(&quot;dmFyIFdlZWsgPSBbJ+UnLCAnACcsICeMJywgJwknLCAn2ycsICeUJywgJ20nXTs=&quot;));\n      eval(atob(&quot;c3RyID0gc3RyLnJlcGxhY2UoL3l5eXl8WVlZWS8sIHRoaXMuZ2V0RnVsbFllYXIoKSk7&quot;));\n      eval(atob(&quot;c3RyID0gc3RyLnJlcGxhY2UoL01NLywgdGhpcy5nZXRNb250aCgpICsgMSA+IDkgPyAodGhpcy5nZXRNb250aCgpICsgMSkudG9TdHJpbmcoKSA6ICcwJyArICh0aGlzLmdldE1vbnRoKCkgKyAxKSk7&quot;));\n      eval(atob(&quot;c3RyID0gc3RyLnJlcGxhY2UoL2RkfERELywgdGhpcy5nZXREYXRlKCkgPiA5ID8gdGhpcy5nZXREYXRlKCkudG9TdHJpbmcoKSA6ICcwJyArIHRoaXMuZ2V0RGF0ZSgpKTs=&quot;));\n      return str;\n    };\n    console.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n*\/<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5904\u7406\u6b65\u9aa4\u5982\u4e0b:<\/p>\n<ol>\n<li>\n<p>\u904d\u5386 FunctionExpression \u8282\u70b9\u3002\u5176\u4e2dpath,node.body \u5373 BlockStatement \u8282\u70b9\u3002<\/p>\n<p>blockStatement.body \u662f\u4e00\u4e2a\u6570\u7ec4,\u91cc\u9762\u662f\u51fd\u6570\u7684\u4ee3\u7801\u884c,\u5b83\u7684\u6bcf\u4e00\u4e2a\u6210\u5458\u5206\u522b\u5bf9\u5e94\u51fd\u6570\u7684\u6bcf\u4e00\u884c\u8bed\u53e5\uff0c\u7136\u540e\u4f7f\u7528\u6570\u7ec4\u7684 map \u65b9\u6cd5\u5bf9\u6bcf\u4e00\u884c\u8bed\u53e5\u5206\u522b\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u662f\u8fd4\u56de\u8bed\u53e5,\u5c31\u4e0d\u505a\u5904\u7406,\u76f4\u63a5\u8fd4\u56de\u539f\u8bed\u53e5\u3002\u5355\u884c\u52a0\u5bc6\u65f6\u4e0d\u80fd\u52a0\u5bc6\u8fd4\u56de\u8bed\u53e5;\u6574\u4e2a\u51fd\u6570\u52a0\u5bc6\u65f6,\u51fd\u6570\u4e2d\u53ef\u4ee5\u6709\u8fd4\u56de\u8bed\u53e5\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 let code = generator(v).code; \u5c06\u8bed\u8f6c\u53d8\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 let cipherText = base64Encode(code);\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n<\/li>\n<li>\n<p>\u6784\u5efa atob('xxxx')\u5f62\u5f0f\u7684\u4ee3\u7801,atob \u662f\u89e3\u5bc6\u51fd\u6570\u540d\u3002\u89e3\u5bc6\u51fd\u6570\u5982\u679c\u4e0d\u662f\u7cfb\u7edf\u81ea\u5e26\u7684,\u8fd8\u9700\u8981\u628a\u89e3\u5bc6\u51fd\u6570\u4e00\u8d77\u653e\u5165\u4ee3\u7801\u4e2d\u3002\u9700\u8981\u751f\u6210 CallExpression,callee \u4e3a\u89e3\u5bc6\u51fd\u6570\u540d\uff0c\u53c2\u6570\u4e3a\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32\u5e38\u91cf,\u8d4b\u503c\u7ed9 decryptFunc\u3002<\/p>\n<\/li>\n<li>\n<p>\u6784\u5efa eval(atob('xxxx'))\u5f62\u5f0f\u7684\u4ee3\u7801,\u9700\u8981\u751f\u6210 CallExpression,callee\u4e3a eval,\u53c2\u6570\u4e3a\u4e0a\u4e00\u6b65\u751f\u6210\u7684 decryptFunc,\u6700\u540e\u7528expressionStatement \u5305\u88f9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u51fd\u6570\u4e2d\u6240\u6709\u8bed\u53e5\u5904\u7406\u5b8c\u540e,\u6784\u5efa\u65b0\u7684 BlockStatement \u66ff\u6362\u539f\u6709\u7684\u5373\u53ef\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u9010\u884c\u52a0\u5bc6\u7684\u65b9\u6848\u4e0d\u5efa\u8bae\u5927\u89c4\u6a21\u5e94\u7528,\u4f1a\u5bfc\u81f4\u6807\u5fd7\u592a\u660e\u663e\u3002\u53ef\u4ee5\u53ea\u9690\u85cf\u5176\u4e2d\u51e0\u53e5\u4ee3\u7801\u6216\u67d0\u4e9b\u53d8\u91cf\u7684\u5173\u952e\u8d4b\u503c\u4f4d\u7f6e\u3002\u81ea\u52a8\u5316\u5904\u7406\u8fc7\u7a0b\u4e2d\u65e0\u6cd5\u77e5\u9053\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u4e3a\u5173\u952e\u4ee3\u7801,\u9664\u975e\u7528\u6237\u5728\u4ee3\u7801\u4e2d\u52a0\u4eba\u6807\u8bb0,\u8868\u793a\u8981\u9690\u85cf\u54ea\u51e0\u53e5\u4ee3\u7801\u3002\u8fd9\u4e2a\u6807\u8bb0\u53ef\u4ee5\u662f\u6ce8\u91ca,\u4f8b\u5982,\u53ef\u4ee5\u5728\u539f\u59cb\u4ee3\u7801\u4e2d\u67d0\u4e00\u53e5\u4ee3\u7801\u540e\u9762\u52a0\u5165\u6ce8\u91ca Base64Encrypt\u3002\u89e3\u6790\u4ee5\u4e0b\u8282\u70b9,\u67e5\u770b\u53d1\u751f\u7684\u53d8\u5316:<\/p>\n<pre><code class=\"language-javascript\">str = str.replace(\/yyyy|YYYY\/\uff0cthis.getFullYear());\/\/Base64Encrypt\n\nNode{\n    type:&#039;ExpressionStatement&#039;,\n    expression: Node{\n        type:&#039;AssignmentExpression&#039;,\n        ...\n        operator:&#039;=&#039;,\n        left: Node{type: &#039;Identifier&#039;, ...name:&#039;str&#039;}\n        right: Node{type:&#039;CallExpression&#039;,...}\n   },\n    trailingComments: [{\n        type:&#039;CommentLine&#039;,\n        value:&#039;Base64Encrypt&#039;,\n        ...\n        }\n    ]\n}<\/code><\/pre>\n<p>\u4ece\u4e0a\u8ff0\u7ed3\u6784\u4e2d\u53ef\u4ee5\u770b\u51fa,\u751f\u6210\u4e86\u4e00\u4e2a trailingComments \u8282\u70b9,\u5b83\u662f\u884c\u5c3e\u6ce8\u91ca\u3002<\/p>\n<p>\u56e0\u6b64\u53ea\u8981\u5728\u9010\u884c\u89e3\u6790\u4ee3\u7801\u65f6,\u6dfb\u52a0\u5224\u65ad\u8bed\u53e5,\u5982\u679c\u6709\u6ce8\u91ca\u4e14\u4e3a Base64Encrypt,\u5c31\u8fdb\u884c\u52a0\u5bc6,<\/p>\n<pre><code class=\"language-javascript\">...\nif(t.isReturnStatement(v)) return v;\nif(!(v.trailingComments &amp;&amp; v.trailingComments[0].value == &#039;Base64Encrypt&#039;)) return v;\ndelete v.trailingComments;\nlet code = generator(v).code;\n...<\/code><\/pre>\n<p>delete\u662fJS \u81ea\u5e26\u7684,\u7528\u6765\u5220\u9664\u5bf9\u8c61\u5c5e\u6027,\u52a0\u5bc6\u4e4b\u524d\u628a\u6ce8\u91ca\u5220\u6389\u3002\u4f7f\u7528JS \u81ea\u5e26\u7684 delete\u800c\u4e0d\u662f path.remove,\u662f\u56e0\u4e3av\u548c v. trailingComments \u90fd\u662f Node \u5bf9\u8c61,\u4e0d\u662f Path \u5bf9\u8c61\u3002<\/p>\n<p>\u5982\u4e0b\u6240\u793a:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    FunctionExpression(path){\n        let blockStatement = path.node.body;\n        let Statements = blockStatement.body.map(function(v){\n            if(t.isReturnStatement(v)) return v;\n            if(!(v.trailingComments &amp;&amp; v.trailingComments[0].value == &#039;Base64Encrypt&#039;)) return v;\n            delete v.trailingComments;\n            let code = generator(v).code;\n            let cipherText = base64Encode(code);\n            let decryptFunc = t.callExpression(t.identifier(&#039;atob&#039;), [t.stringLiteral(cipherText)]);\n            return t.expressionStatement(t.callExpression(t.identifier(&#039;eval&#039;), [decryptFunc]));\n        });\n        path.get(&#039;body&#039;).replaceWith(t.blockStatement(Statements));\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});<\/code><\/pre>\n<h3>3.\u4ee3\u7801\u7684\u9010\u884cASCII\u7801\u6df7\u6dc6<\/h3>\n<p>\u8fd9\u79cd\u65b9\u6848\u7684\u539f\u7406\u4e0e\u4ee3\u7801\u7684\u9010\u884c\u52a0\u5bc6\u6ca1\u6709\u592a\u5927\u5dee\u522b,\u53ea\u4e0d\u8fc7\u53bb\u6389\u4e86\u5b57\u7b26\u4e32\u52a0\u5bc6\u51fd\u6570,\u6539\u7528charCodeAt\u5c06\u5b57\u7b26\u4e32\u8f6c\u5230ASCII\u7801,\u628a\u89e3\u5bc6\u51fd\u6570\u6362\u6210 String.fromCharCode,\u6700\u540e\u4f7f\u7528eval\u51fd\u6570\u6765\u6267\u884c\u5b57\u7b26\u4e32\u4ee3\u7801\u3002\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    FunctionExpression(path){\n        let blockStatement = path.node.body;\n        let Statements = blockStatement.body.map(function(v){\n            if(t.isReturnStatement(v)) return v;\n            if(!(v.trailingComments &amp;&amp; v.trailingComments[0].value == &#039;ASCIIEncrypt&#039;)) return v;\n            delete v.trailingComments;\n            let code = generator(v).code;\n            let codeAscii = [].map.call(code, function(v){\n                return t.numericLiteral(v.charCodeAt(0));\n            });\n            let decryptFuncName = t.memberExpression(t.identifier(&#039;String&#039;), t.identifier(&#039;fromCharCode&#039;));\n            let decryptFunc = t.callExpression(decryptFuncName, codeAscii);\n            return t.expressionStatement(t.callExpression(t.identifier(&#039;eval&#039;), [decryptFunc]));\n        });\n        path.get(&#039;body&#039;).replaceWith(t.blockStatement(Statements));\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\nDate.prototype.format = function (formatStr) {\n  var str = formatStr;\n  var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;];\n  str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n  str = str.replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : &#039;0&#039; + (this.getMonth() + 1));\n  str = str.replace(\/dd|DD\/, this.getDate() &gt; 9 ? this.getDate().toString() : &#039;0&#039; + this.getDate());\n  return str;\n};\nconsole.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n*\/<\/code><\/pre>\n<p>\u5b57\u7b26\u4e32\u5728JS \u4e2d\u4e5f\u662f\u6570\u7ec4,\u4e0d\u8fc7\u662f\u53ea\u8bfb\u7684\u3002\u4f46\u662f\u5b83\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u6570\u7ec4\u7684 map,\u6240\u4ee5\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7528[].map.call\u6765\u95f4\u63a5\u8c03\u7528,\u4f5c\u7528\u662f\u4e00\u6837\u7684\u3002\u628a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u6210\u5458\u8f6c\u6210 ASCII\u7801\u4e4b\u540e\uff0c\u7528 NumericLiteral \u8282\u70b9\u6765\u5305\u88f9\uff0c\u7136\u540e\u751f\u6210\u4e00\u4e2a MemberExpression \u8282\u70b9\uff0cString.fromCharCode\u4f5c\u4e3a\u89e3\u5bc6\u51fd\u6570\u540d\u3002<\/p>\n<p>\u4e0d\u7ba1\u662f\u4ee3\u7801\u9010\u884c\u52a0\u5bc6\u8fd8\u662f\u4ee3\u7801\u9010\u884c ASCII\u6df7\u6dc6,\u90fd\u8981\u5728\u6807\u8bc6\u7b26\u6df7\u6dc6\u4e4b\u540e\u3002\u7531\u6b64\u53ef\u89c1\uff0c<br \/>\n\u5728\u505a\u6807\u8bc6\u7b26\u6df7\u6dc6\u65f6\uff0c\u5e94\u8be5\u5904\u7406\u539f\u59cb\u4ee3\u7801\u4e2d\u7684eval\u548c Function\u3002<\/p>\n<h2>\u56db\u3001\u5b8c\u6574\u7684\u4ee3\u7801\u5904\u7406\u540e\u7684\u6548\u679c<\/h2>\n<p>\u672c\u7ae0\u524d\u4e09\u8282\u4ecb\u7ecd\u4e86\u591a\u79cd\u6df7\u6dc6\u65b9\u6848\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5176\u4e2d\u6709\u4e9b\u65b9\u6848\u8054\u5408\u4f7f\u7528\u65f6,\u9700\u8981\u4fee\u6539\u90e8\u5206\u4ee3\u7801,\u672c\u8282\u4ecb\u7ecd\u5b8c\u6574\u7684\u4ee3\u7801\u4ee5\u53ca\u5b8c\u6574\u5904\u7406\u540e\u7684\u6548\u679c\u3002<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\n\/\/\u628a\u6df7\u6dc6\u65b9\u6848\u7684\u76f8\u5173\u5b9e\u73b0\u65b9\u6cd5\u5c01\u88c5\u6210\u7c7b\nfunction ConfoundUtils(ast, encryptFunc){\n    this.ast = ast;\n    this.bigArr = [];\n    \/\/\u63a5\u6536\u4f20\u8fdb\u6765\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5b57\u7b26\u4e32\u52a0\u5bc6\n    this.encryptFunc = encryptFunc;\n}\n\/\/\u6539\u53d8\u5bf9\u8c61\u5c5e\u6027\u8bbf\u95ee\u65b9\u5f0f console.log \u6539\u4e3a console[&quot;log&quot;]\nConfoundUtils.prototype.changeAccessMode = function (){\n    traverse(this.ast, {\n        MemberExpression(path){\n            if(t.isIdentifier(path.node.property)){\n                let name = path.node.property.name;\n                path.node.property = t.stringLiteral(name);\n            }\n            path.node.computed = true;\n        }, \n    });\n}\n\/\/\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\u7684\u5904\u7406\nConfoundUtils.prototype.changeBuiltinObjects = function (){\n    traverse(this.ast, {\n        Identifier(path){\n            let name = path.node.name;\n    if(&#039;eval|parseInt|encodeURIComponent|Object|Function|Boolean|Number|Math|Date|String|RegExp|Array&#039;.indexOf(name) != -1){\n                path.replaceWith(t.memberExpression(t.identifier(&#039;window&#039;),                                                  t.stringLiteral(name), true));\n            }\n        }\n    });\n}\n\/\/\u6570\u503c\u5e38\u91cf\u52a0\u5bc6\nConfoundUtils.prototype.numericEncrypt = function (){\n    traverse(this.ast, {\n        NumericLiteral(path){\n            let value = path.node.value;\n            let key = parseInt(Math.random() * (999999 - 100000) + 100000, 10);\n            let cipherNum = value ^ key;\n            path.replaceWith(t.binaryExpression(&#039;^&#039;,                                                                  t.numericLiteral(cipherNum),                                                t.numericLiteral(key)));\n            path.skip();\n        }\n    });\n}\n\/\/\u5b57\u7b26\u4e32\u52a0\u5bc6\u4e0e\u6570\u7ec4\u6df7\u6dc6\nConfoundUtils.prototype.arrayConfound = function (){\n    let bigArr = [];\n    let encryptFunc = this.encryptFunc;\n    traverse(this.ast, {\n        StringLiteral(path){\n            let bigArrIndex = bigArr.indexOf(encryptFunc(path.node.value));\n            let index = bigArrIndex;\n            if(bigArrIndex == -1){\n                let length = bigArr.push(encryptFunc(path.node.value));\n                index = length -1;\n            }\n            let encStr = t.callExpression(\n                t.identifier(&#039;atob&#039;), \n                [t.memberExpression(t.identifier(&#039;arr&#039;),                                   t.numericLiteral(index), true)]);\n            path.replaceWith(encStr);\n      }\n    });\n    bigArr = bigArr.map(function(v){\n      return t.stringLiteral(v);\n    });\n    this.bigArr = bigArr;\n}\n\/\/\u6570\u7ec4\u4e71\u5e8f\nConfoundUtils.prototype.arrayShuffle = function (){\n    (function(myArr, num){\n        var xiaojianbang = function(nums){\n            while(--nums){\n                myArr.unshift(myArr.pop());\n            }\n        };\n        xiaojianbang(++num);\n    }(this.bigArr, 0x10));\n}\n\/\/\u4e8c\u9879\u5f0f\u8f6c\u51fd\u6570\u82b1\u6307\u4ee4\nConfoundUtils.prototype.binaryToFunc = function (){\n    traverse(this.ast, {\n        BinaryExpression(path){\n            let operator = path.node.operator;\n            let left = path.node.left;\n            let right = path.node.right;\n            let a = t.identifier(&#039;a&#039;);\n            let b = t.identifier(&#039;b&#039;);\n            let funcNameIdentifier = path.scope.generateUidIdentifier(&#039;xxx&#039;);\n            let func = t.functionDeclaration(\n                funcNameIdentifier, \n                [a, b], \n                t.blockStatement([t.returnStatement(\n                    t.binaryExpression(operator, a, b)\n                    )]));\n            let BlockStatement = path.findParent(\nfunction(p){return p.isBlockStatement()});\n            BlockStatement.node.body.unshift(func);\n            path.replaceWith(t.callExpression(\nfuncNameIdentifier, [left, right]));\n        }\n    });\n}\n\/\/\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\nConfoundUtils.prototype.stringToHex = function (){\n    function hexEnc(code) {\n        for (var hexStr = [], i = 0, s; i &lt; code.length; i++) {\n            s = code.charCodeAt(i).toString(16);\n            hexStr += &quot;\\\\x&quot; + s;\n        }\n        return hexStr\n    }\n    traverse(this.ast,\u00a0{\n        MemberExpression(path){\n            if(t.isIdentifier(path.node.property)){\n                let\u00a0name\u00a0=\u00a0path.node.property.name;\n                path.node.property\u00a0=\u00a0t.stringLiteral(hexEnc(name));\n            }\n            path.node.computed\u00a0=\u00a0true;\n        }\n    });\n}\n\/\/\u6807\u8bc6\u7b26\u6df7\u6dc6\nConfoundUtils.prototype.renameIdentifier = function (){\n    \/\/\u6807\u8bc6\u7b26\u6df7\u6dc6\u4e4b\u524d\u5148\u8f6c\u6210\u4ee3\u7801\u518d\u89e3\u6790\uff0c\u624d\u80fd\u786e\u4fdd\u65b0\u751f\u6210\u7684\u4e00\u4e9b\u8282\u70b9\u4e5f\u88ab\u89e3\u6790\u5230\n    let code = generator(this.ast).code;\n    let newAst = parser.parse(code);\n    \/\/\u751f\u6210\u6807\u8bc6\u7b26\n    function generatorIdentifier(decNum){\n        let arr = [&#039;O&#039;, &#039;o&#039;, &#039;0&#039;];\n        let retval = [];\n        while(decNum &gt; 0){\n            retval.push(decNum % 3);\n            decNum = parseInt(decNum \/ 3);\n        }\n        let Identifier = retval.reverse().map(function(v){\n            return arr[v]\n        }).join(&#039;&#039;);\n        Identifier.length &lt; 6 ? (Identifier = (&#039;OOOOOO&#039; + Identifier).substr(-6)):\n        Identifier[0] == &#039;0&#039; &amp;&amp; (Identifier = &#039;O&#039; + Identifier);\n        return Identifier;\n    }\n    function\u00a0renameOwnBinding(path){\n        let OwnBindingObj = {}, globalBindingObj = {}, i = 0;\n        path.traverse({\n            Identifier(p){\n                let name = p.node.name;\n                let binding = p.scope.getOwnBinding(name);\n                binding &amp;&amp; generator(binding.scope.block).code == path + &#039;&#039; ? \n                (OwnBindingObj[name] = binding) : (globalBindingObj[name] = 1);\n            }\n        });\n        for(let oldName in OwnBindingObj){\n            do{\n                var newName = generatorIdentifier(i++);\n            }while(globalBindingObj[newName]);\n            OwnBindingObj[oldName].scope.rename(oldName, newName);\n        }\n    }\n    traverse(newAst, {\n        &#039;Program|FunctionExpression|FunctionDeclaration&#039;(path) {\n            renameOwnBinding(path);\n        }\n    });\n    this.ast = newAst;\n}\n\/\/\u6307\u5b9a\u4ee3\u7801\u884c\u52a0\u5bc6\nConfoundUtils.prototype.appointedCodeLineEncrypt = function (){\n    traverse(this.ast, {\n        FunctionExpression(path){\n            let blockStatement = path.node.body;\n            let Statements = blockStatement.body.map(function(v){\n                if(t.isReturnStatement(v)) return v;\n                if(!(v.trailingComments &amp;&amp; v.trailingComments[0].value == &#039;Base64Encrypt&#039;)) return v;\n                delete v.trailingComments;\n                let code = generator(v).code;\n                let cipherText = base64Encode(code);\n                let decryptFunc = t.callExpression(t.identifier(&#039;atob&#039;),                                                   [t.stringLiteral(cipherText)]);\n                return t.expressionStatement(\nt.callExpression(t.identifier(&#039;eval&#039;), [decryptFunc]));\n            });\n            path.get(&#039;body&#039;).replaceWith(t.blockStatement(Statements));\n        }\n    });\n}\n\/\/\u6307\u5b9a\u4ee3\u7801\u884cASCII\u7801\u6df7\u6dc6\nConfoundUtils.prototype.appointedCodeLineAscii = function (){\n    traverse(this.ast, {\n        FunctionExpression(path){\n            let blockStatement = path.node.body;\n            let Statements = blockStatement.body.map(function(v){\n                if(t.isReturnStatement(v)) return v;\n                if(!(v.trailingComments &amp;&amp; v.trailingComments[0].value == &#039;ASCIIEncrypt&#039;)) return v;\n                delete v.trailingComments;\n                let code = generator(v).code;\n                let codeAscii = [].map.call(code, function(v){\n                    return t.numericLiteral(v.charCodeAt(0));\n                })\n                let decryptFuncName = t.memberExpression(\nt.identifier(&#039;String&#039;), t.identifier(&#039;fromCharCode&#039;));\n                let decryptFunc = t.callExpression(decryptFuncName, codeAscii);\n                return t.expressionStatement(\nt.callExpression(t.identifier(&#039;eval&#039;),[decryptFunc]));\n            });\n            path.get(&#039;body&#039;).replaceWith(t.blockStatement(Statements));\n        }\n    });\n}\n\/\/\u6784\u5efa\u6570\u7ec4\u58f0\u660e\u8bed\u53e5\uff0c\u52a0\u5165\u5230ast\u6700\u524d\u9762\nConfoundUtils.prototype.unshiftArrayDeclaration = function(){\n    this.bigArr = t.variableDeclarator(t.identifier(&#039;arr&#039;), t.arrayExpression(this.bigArr));\n    this.bigArr = t.variableDeclaration(&#039;var&#039;, [this.bigArr]);\n    this.ast.program.body.unshift(this.bigArr);\n}\n\/\/\u62fc\u63a5\u4e24\u4e2aast\u7684body\u90e8\u5206\nConfoundUtils.prototype.astConcatUnshift = function(ast){\n    this.ast.program.body.unshift(ast);\n}\nConfoundUtils.prototype.getAst = function(){\n    return this.ast;\n}\n\/\/Base64\u7f16\u7801\nfunction base64Encode(e) {\n    var r, a, c, h, o, t, base64EncodeChars = &#039;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/&#039;;\n    for (c = e.length, a = 0, r = &#039;&#039;; a &lt; c;) {\n        if (h = 255 &amp; e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4),\n            r += &#039;==&#039;;\n            break\n        }\n        if (o = e.charCodeAt(a++), a == c) {\n            r += base64EncodeChars.charAt(h &gt;&gt; 2),\n            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n            r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2),\n            r += &#039;=&#039;;\n            break\n        }\n        t = e.charCodeAt(a++),\n        r += base64EncodeChars.charAt(h &gt;&gt; 2),\n        r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),\n        r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2 | (192 &amp; t) &gt;&gt; 6),\n        r += base64EncodeChars.charAt(63 &amp; t)\n    }\n    return r\n}\nfunction main(){\n    \/\/\u8bfb\u53d6\u8981\u6df7\u6dc6\u7684\u4ee3\u7801\n    const jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\n    \/\/\u8bfb\u53d6\u8fd8\u539f\u6570\u7ec4\u4e71\u5e8f\u7684\u4ee3\u7801\n    const jscodeFront = fs.readFileSync(&quot;.\/demoFront.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\n    \/\/\u628a\u8981\u6df7\u6dc6\u7684\u4ee3\u7801\u89e3\u6790\u6210ast\n    let ast = parser.parse(jscode);\n    \/\/\u628a\u8fd8\u539f\u6570\u7ec4\u4e71\u5e8f\u7684\u4ee3\u7801\u89e3\u6790\u6210astFront\n    let astFront = parser.parse(jscodeFront);\n    \/\/\u521d\u59cb\u5316\u7c7b\uff0c\u4f20\u9012\u81ea\u5b9a\u4e49\u7684\u52a0\u5bc6\u51fd\u6570\u8fdb\u53bb\n    let confoundAst = new ConfoundUtils(ast, base64Encode);\n    let confoundAstFront = new ConfoundUtils(astFront);\n    \/\/\u6539\u53d8\u5bf9\u8c61\u5c5e\u6027\u8bbf\u95ee\u65b9\u5f0f\n    confoundAst.changeAccessMode();\n    \/\/\u6807\u51c6\u5185\u7f6e\u5bf9\u8c61\u7684\u5904\u7406\n    confoundAst.changeBuiltinObjects();\n    \/\/\u4e8c\u9879\u5f0f\u8f6c\u51fd\u6570\u82b1\u6307\u4ee4\n    confoundAst.binaryToFunc()\n    \/\/\u5b57\u7b26\u4e32\u52a0\u5bc6\u4e0e\u6570\u7ec4\u6df7\u6dc6\n    confoundAst.arrayConfound();\n    \/\/\u6570\u7ec4\u4e71\u5e8f\n    confoundAst.arrayShuffle();\n\n    \/\/\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u4ee3\u7801\uff0c\u6539\u53d8\u5bf9\u8c61\u5c5e\u6027\u8bbf\u95ee\u65b9\u5f0f\uff0c\u5bf9\u5176\u4e2d\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u5341\u516d\u8fdb\u5236\u7f16\u7801\n    confoundAstFront.stringToHex();\n    astFront = confoundAstFront.getAst();\n    \/\/\u5148\u628a\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\uff0c\u52a0\u5165\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u7684ast\u4e2d\n    confoundAst.astConcatUnshift(astFront.program.body[0]);\n\n    \/\/\u518d\u751f\u6210\u6570\u7ec4\u58f0\u660e\u8bed\u53e5\uff0c\u5e76\u52a0\u5165\u5230\u88ab\u6df7\u6dc6\u4ee3\u7801\u7684\u6700\u5f00\u59cb\u5904\n    confoundAst.unshiftArrayDeclaration();\n    \/\/\u6807\u8bc6\u7b26\u91cd\u547d\u540d\n    confoundAst.renameIdentifier();\n    \/\/\u6307\u5b9a\u4ee3\u7801\u884c\u7684\u6df7\u6dc6\uff0c\u9700\u8981\u653e\u5230\u6807\u8bc6\u7b26\u6df7\u6dc6\u4e4b\u540e\n    confoundAst.appointedCodeLineEncrypt();\n    confoundAst.appointedCodeLineAscii();\n    \/\/\u6570\u503c\u5e38\u91cf\u6df7\u6dc6\n    confoundAst.numericEncrypt();\n    ast = confoundAst.getAst();\n    \/\/ast\u8f6c\u4e3a\u4ee3\u7801\n    code = generator(ast).code;\n    \/\/\u6df7\u6dc6\u7684\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u6709\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u52a0\u5bc6\uff0cast\u8f6c\u6210\u4ee3\u7801\u4ee5\u540e\u4f1a\u6709\u591a\u4f59\u7684\u8f6c\u4e49\u5b57\u7b26\uff0c\u9700\u8981\u66ff\u6362\u6389\n    code = code.replace(\/\\\\\\\\x\/g,\u00a0&#039;\\\\x&#039;);\n    console.log(code);\n}\nmain();<\/code><\/pre>\n<p>\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u8fdb\u884c\u4ee5\u4e0b\u4e09\u70b9\u8bf4\u660e\u3002<br \/>\n<strong>(1)<\/strong> demo.js \u4e2d\u5b58\u653e\u7684\u4ee3\u7801\u7528\u6765\u6d4b\u8bd5\u6df7\u6dc6\u65b9\u6848,\u4ee3\u7801\u4e3a\u539f\u59cb\u4ee3\u7801\u3002<br \/>\n<strong>(2)<\/strong> demoFront.js \u4e2d\u5b58\u653e\u7684\u4ee3\u7801\u7528\u4e8e\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f,2.4 \u8282\u4e2d\u3002<br \/>\n<strong>(3) <\/strong>\u5982\u679c\u4e0d\u4f7f\u7528\u6d4f\u89c8\u5668\u81ea\u5e26\u7684 atob \u4f5c\u4e3a\u89e3\u5bc6\u51fd\u6570\uff0c\u5c31\u9700\u8981\u5b9e\u73b0\u5bf9\u5e94\u7684 Base64 \u89e3\u7801\u51fd\u6570,\u7136\u540e\u4e0e\u8fd8\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u4ee3\u7801\u4e00\u8d77\u52a0\u5165\u3002<\/p>\n<p>\u7528\u4e0a\u8ff0\u4ee3\u7801\u5904\u7406\u539f\u59cb\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code class=\"language-javascript\">var OOOOOO = [&quot;Zm9ybWF0&quot;, &quot;5Q==&quot;, &quot;AA==&quot;, &quot;jA==&quot;, &quot;CQ==&quot;, &quot;2w==&quot;, &quot;lA==&quot;, &quot;bQ==&quot;, &quot;cmVwbGFjZQ==&quot;, &quot;Z2V0RnVsbFllYXI=&quot;, &quot;Z2V0TW9udGg=&quot;, &quot;dG9TdHJpbmc=&quot;, &quot;MA==&quot;, &quot;Z2V0RGF0ZQ==&quot;, &quot;bG9n&quot;, &quot;eXl5eS1NTS1kZA==&quot;, &quot;RGF0ZQ==&quot;, &quot;cHJvdG90eXBl&quot;];\n(function (OOOOOO, OOOOOo) {\n  var OOOOO0 = function (OOOOOo) {\n    while (--OOOOOo) {\n      OOOOOO[&quot;\\x70\\x75\\x73\\x68&quot;](OOOOOO[&quot;\\x73\\x68\\x69\\x66\\x74&quot;]());\n    }\n  };\n  OOOOO0(++OOOOOo);\n})(OOOOOO, 142584 ^ 142568);\nwindow[atob(OOOOOO[640621 ^ 640621])][atob(OOOOOO[333872 ^ 333873])][atob(OOOOOO[534827 ^ 534825])] = function (OOOOOo) {\n  function OOOoOo(OOOOOO, OOOOOo) {\n    return OOOOOO + OOOOOo;\n  }\n  function OOOoOO(OOOOOO, OOOOOo) {\n    return OOOOOO &gt; OOOOOo;\n  }\n  function OOOO00(OOOOOO, OOOOOo) {\n    return OOOOOO + OOOOOo;\n  }\n  function OOOO0o(OOOOOO, OOOOOo) {\n    return OOOOOO + OOOOOo;\n  }\n  function OOOO0O(OOOOOO, OOOOOo) {\n    return OOOOOO + OOOOOo;\n  }\n  function OOOOo0(OOOOOO, OOOOOo) {\n    return OOOOOO + OOOOOo;\n  }\n  function OOOOoo(OOOOOO, OOOOOo) {\n    return OOOOOO &gt; OOOOOo;\n  }\n  var OOOOO0 = OOOOOo;\n  var OOOOoO = [atob(OOOOOO[889992 ^ 889995]), atob(OOOOOO[199115 ^ 199119]), atob(OOOOOO[393974 ^ 393971]), atob(OOOOOO[915745 ^ 915751]), atob(OOOOOO[302842 ^ 302845]), atob(OOOOOO[560371 ^ 560379]), atob(OOOOOO[624550 ^ 624559])];\n  OOOOO0 = OOOOO0[atob(OOOOOO[449616 ^ 449626])](\/yyyy|YYYY\/, this[atob(OOOOOO[445092 ^ 445103])]());\n  OOOOO0 = OOOOO0[atob(OOOOOO[314857 ^ 314851])](\/MM\/, OOOOoo(OOOOo0(this[atob(OOOOOO[568473 ^ 568469])](), 666066 ^ 666067), 271298 ^ 271307) ? OOOO0O\n(this[atob(OOOOOO[251391 ^ 251379])](), 425338 ^ 425339)[atob(OOOOOO[357360 ^ 357373])]() : OOOO0o(atob(OOOOOO[391630 ^ 391616]), OOOO00(this[atob(OOOOOO[391880 ^ 391876])](), 812408 ^ 812409)));\n  OOOOO0 = OOOOO0[atob(OOOOOO[666436 ^ 666446])](\/dd|DD\/, OOOoOO(this[atob(OOOOOO[189039 ^ 189024])](), 863695 ^ 863686) ? this[atob(OOOOOO[777680 ^ 777695])]()[atob(OOOOOO[542837 ^ 542840])]() : OOOoOo(atob(OOOOOO[250977 ^ 250991]), this[atob(OOOOOO[785111 ^ 785112])]()));\n  return OOOOO0;\n};\nconsole[atob(OOOOOO[348315 ^ 348299])](new window[atob(OOOOOO[247633 ^ 247633])]()[atob(OOOOOO[400280 ^ 400282])](atob(OOOOOO[406239 ^ 406222])));<\/code><\/pre>\n<blockquote>\n<p>\u4ed6\u5988\u7684\u6548\u679c\u786e\u5b9e\u70b8\u88c2\uff0c\u4eb2\u5988\u4e0d\u8ba4\u3002<\/p>\n<\/blockquote>\n<h2>\u4e94\u3001\u4ee3\u7801\u6267\u884c\u903b\u8f91\u7684\u6df7\u6dc6<\/h2>\n<h3>1.\u5b9e\u73b0\u6d41\u7a0b\u5e73\u5766\u5316<\/h3>\n<p>\u628a\u4ee5\u4e0b\u4ee3\u7801\u653e\u5230AST Explorer \u7f51\u7ad9\u4e2d\u89e3\u6790,\u5bf9\u7167\u7f51\u7ad9\u6765\u5b9e\u73b0\u76f8\u5e94\u7684\u6d41\u7a0b\u5e73\u5766\u5316,\u5982\u4e0b\u6240\u793a:<\/p>\n<pre><code class=\"language-javascript\">Date.prototype.format = function (formatStr){\n    let array=&quot;0|1|4|5|3|2&quot;.split(&quot;|&quot;),\n        index =0;\n    while(!![]){\n        switch ( + _array[_index++]){\n            case 0:\n                var str = formatStr;\n                continue;\n            case 1:\n                var Week = [&#039;\u65e5&#039;,&#039;\u4e00&#039;, &#039;\u4e8c&#039;,&#039;\u4e09&#039;,&#039;\u56db&#039;,&#039;\u4e94&#039;,&#039;\u516d&#039;];\n                continue;\n            case 2:\n                return str;\n                continue;\n            case 3:\n                str = str.replace(\/dd|DD\/,this.getDate() &gt; 9 ? this.getDate().toString():&#039;0&#039; + this.getDate());\n                continue;\n            case 4:\n                str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n                continue;\n            case 5:\n                str = str.replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : &#039;0&#039; + (this.getMonth() + 1));\n                continue;\n        }\n        break;\n    }\n};\nconsole.log(new Date().format( &#039;yyyy -MM- dd&#039;));<\/code><\/pre>\n<p>\u56e0\u4e3a\u91c7\u7528\u7684\u662f\u4e00\u884c\u8bed\u53e5\u5bf9\u5e94\u4e00\u6761 case \u7684\u65b9\u6848,\u6240\u4ee5\u9700\u8981\u5148\u83b7\u53d6\u6bcf\u4e00\u884c\u8bed\u53e5,\u5904\u7406\u65b9\u6cd5\u4e0e\u4e4b\u524d\u4ecb\u7ecd\u7684\u4ee3\u7801\u9010\u884c\u6df7\u6dc6\u4e00\u81f4\u3002\u904d\u5386 FunctionExpression \u8282\u70b9,\u83b7\u53d6 path.node.body\u5373BlockStatement\u3002\u8be5\u8282\u70b9\u7684 body \u5c5e\u6027\u662f\u4e00\u4e2a\u6570\u7ec4,\u901a\u8fc7 map \u904d\u5386\u6570\u7ec4\uff0c\u5373\u53ef\u64cd\u4f5c\u5176\u4e2d\u7684\u6bcf\u4e00\u884c\u8bed\u53e5\u3002\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">traverse(ast,{\n    FunctionExpression(path) {\n        let blockStatement = path.node.body;\n        let Statements = blockStatement.body.map(function (v, i) {\n            return {index: i, value: v}\n        });\n    }});<\/code><\/pre>\n<p>\u5728\u6d41\u7a0b\u5e73\u5766\u5316\u7684\u6df7\u6dc6\u4e2d,\u8981\u6253\u4e71\u539f\u5148\u7684\u8bed\u53e5\u987a\u5e8f,\u4f46\u662f\u5728\u6267\u884c\u65f6\u53c8\u8981\u6309\u539f\u5148\u7684\u987a\u5e8f\u6267\u884c,\u56e0\u6b64\u5728\u6253\u4e71\u8bed\u53e5\u987a\u5e8f\u4e4b\u524d,\u8981\u5bf9\u539f\u5148\u7684\u8bed\u53e5\u987a\u5e8f\u505a\u6620\u5c04\u3002<\/p>\n<p>\u4ece\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa,\u91c7\u7528<code>{index:i\uff0cvalue:v}<\/code>\u8fd9\u79cd\u65b9\u5f0f\u6765\u505a\u6620\u5c04\u3002<\/p>\n<p>index \u662f\u8bed\u53e5\u5728 blockStatement.body \u4e2d\u7684\u7d22\u5f15\u4e5f\u5c31\u662f\u539f\u5148\u7684\u987a\u5e8f\u3002value \u662f\u8bed\u53e5\u672c\u8eab\u3002\u6709\u4e86\u5bf9\u5e94\u7684\u5173\u7cfb\u540e,\u5c31\u53ef\u4ee5\u65b9\u4fbf\u5730\u5efa\u7acb\u5206\u53d1\u5668,\u6765\u63a7\u5236\u4ee3\u7801\u5728\u6267\u884c\u65f6\u8df3\u8f6c\u5230\u6b63\u786e\u7684 case \u8bed\u53e5\u5757\u3002<\/p>\n<p>\u63a5\u7740\u8981\u6253\u4e71\u8bed\u53e5\u987a\u5e8f,\u7b97\u6cd5\u5f88\u7b80\u5355,\u904d\u5386\u6570\u7ec4\u6bcf\u4e00\u4e2a\u6210\u5458,\u6bcf\u4e00\u6b21\u5faa\u73af\u968f\u673a\u53d6\u51fa\u4e00\u4e2a\u7d22\u5f15\u4e3aj\u7684\u6210\u5458,\u4e0e\u7d22\u5f15\u4e3ai\u7684\u6210\u5458\u8fdb\u884c\u4ea4\u6362\u3002\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">\/\/\u6253\u4e71\u8bed\u53e5\u987a\u5e8f\nlet i = Statements.ength;\nwhile(i){\n    let j = Math.floor(Math.random() * i--);\n    [Statements[j],Statements[i]] = [Statements[i], Statements[j]];\n}<\/code><\/pre>\n<p>\u63a5\u7740\u6784\u5efa case \u8bed\u53e5\u5757,\u5728 AST \u4e2d\u5b83\u7684\u7c7b\u578b\u4e3a SwitchCase\u3002<\/p>\n<p>\u4e0b\u9762\u4ecb\u7ecd SwitchCase \u7684\u7ed3\u6784:<\/p>\n<pre><code class=\"language-javascript\">{\n    type:&#039;SwitchCase&#039;,\n    test:{ type: &#039;NumericLiteral&#039;, value: 5},\n    consequent:[\n        Node { ...}\uff0c\n        {type: &#039;ContinueStatement&#039;,label: null}\n    ]\n}<\/code><\/pre>\n<p>test \u662f case \u540e\u7684\u503c,consequent \u662f\u4e00\u4e2a\u6570\u7ec4,\u5b58\u653e case \u8bed\u5757\u4e2d\u5177\u4f53\u7684\u8bed\u53e5,\u6240\u4ee5 case\u8bed\u53e5\u5757\u7684\u751f\u6210\u5f88\u5bb9\u6613\u3002\u904d\u5386\u6253\u4e71\u987a\u5e8f\u540e\u7684\u8bed\u53e5\u6570\u7ec4 Statements\u3002SwitchCase \u4e2d\u7684 test \u7528\u4ece0\u5f00\u59cb\u9012\u589e\u7684 NumericLiteral, consequent \u5b58\u653e\u4e24\u6761\u8bed\u53e5,\u4e00\u6761\u662f\u539f\u59cb\u4ee3\u7801\u4e2d\u7684\u8bed\u53e5,\u53e6\u4e00\u6761\u662f continue \u8bed\u53e5\u3002\u6700\u540e\u628a\u5305\u88c5\u597d\u7684 SwitchCase \u653e\u5165 cases \u6570\u7ec4\u4e2d\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">let cases = [];\nStatements.map(function(v\uff0ci){\n    let switchCase = t.switchCase(t.numericLiteral(i), [v.value, t.continueStatement()]);\n    cases.push(switchCase);\n]);<\/code><\/pre>\n<p>\u4e3a\u4e86\u63a7\u5236 switch \u6bcf\u4e00\u6b21\u90fd\u8df3\u8f6c\u5230\u6b63\u786e\u7684 case \u8bed\u53e5\u5757\u4e0a,\u9700\u8981\u6784\u5efa\u5206\u53d1\u5668\u3002\u5206\u53d1\u5668\u4e2d\u7684&quot;0|1|4|5|3|2&quot;\u6765\u6e90\u4e8e\u4e4b\u524d\u505a\u7684\u6620\u5c04\u3002\u8c03\u6574\u4e0a\u9762\u7528\u6765\u751f\u6210 case \u8bed\u53e5\u5757\u7684\u4ee3\u7801,\u6539\u4e3a\u5982\u4e0b\u5f62\u5f0f:<\/p>\n<pre><code class=\"language-javascript\">let dispenserArr = [];\nlet cases =[];\nStatements.map(function(v, i){\n    dispenserArr[v.index] = i;\n    let switchCase = t.switchCase(t.numericLiteral(i), [v.value, t.continueStatement()]);\n    cases.push(switchCase);\n});\nlet dispenserStr = dispenserArr.join(&#039;|&#039;);<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53c8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u7ec4 dispenserArr,\u5e76\u4e14\u8be5\u6570\u7ec4\u5728\u6700\u540e\u4f7f\u7528 join,\u4ee5\u201c|\u201d\u4f5c\u4e3a\u8fde\u63a5\u7b26\u8fde\u63a5\u6570\u7ec4\u6240\u6709\u6210\u5458\u3002\u53ef\u4ee5\u770b\u51fa,dispenserStr \u5c31\u662f\u5206\u53d1\u5668\u4e2d\u7684&quot;0|1|4|5|3|2&quot;,\u800c014532 \u662f\u4ee3\u7801\u6267\u884c\u7684\u771f\u5b9e\u987a\u5e8f,\u56e0\u6b64 dispenserArr \u4e2d\u7684\u6210\u5458\u5e94\u5f53\u662f case \u540e\u9762\u7684\u503c\u3002\u5047\u8bbecase \u540e\u9762\u7684\u503c\u662f 5,\u5373i\u4e3a5,\u518d\u53d6\u51fa\u5f53\u524d\u8bed\u53e5\u7684\u771f\u5b9e\u7d22\u5f15 v.index,\u5047\u8bbe\u5b83\u662f 3,\u4e5f\u5c31\u662f\u539f\u59cb\u4ee3\u7801\u4e2d\u8be5\u8bed\u53e5\u662f\u7b2c4\u884c\u8bed\u53e5(\u6570\u7ec4\u4ece0\u5f00\u59cb)\u3002\u90a3\u4e48\u5c31\u628a dispenserArr \u4e2d\u7d22\u5f15\u4e3a3\u7684\u6210\u5458\u6539\u4e3a5\u3002\u5bf9\u5e94\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684 dispenserArr[v.index]=i,\u5373\u53ef\u751f\u6210\u5206\u53d1\u5668\u4e2d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n\u6838\u5fc3\u90e8\u5206\u5df2\u7ecf\u4ecb\u7ecd\u5b8c\u6bd5,\u63a5\u4e0b\u6765\u7528 types \u7ec4\u4ef6\u751f\u6210\u8282\u70b9\u3002\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    FunctionExpression(path){\n        let blockStatement = path.node.body;\n        \/\/\u9010\u884c\u63d0\u53d6\u8bed\u53e5\uff0c\u6309\u539f\u5148\u7684\u8bed\u53e5\u987a\u5e8f\u5efa\u7acb\u7d22\u5f15\uff0c\u5305\u88c5\u6210\u5bf9\u8c61\n        let Statements = blockStatement.body.map(function(v, i){\n            return {index: i, value: v};\n        });\n        \/\/\u6d17\u724c\uff0c\u6253\u4e71\u8bed\u53e5\u987a\u5e8f\n        let i = Statements.length;\n        while(i){\n            let j = Math.floor(Math.random() * i--);\n            [Statements[j], Statements[i]] = [Statements[i], Statements[j]];\n        }\n        \/\/\u6784\u5efa\u5206\u53d1\u5668\uff0c\u521b\u5efaswitchCase\u6570\u7ec4\n        let dispenserArr = [];\n        let cases = [];\n        Statements.map(function(v, i){\n            dispenserArr[v.index] = i;\n            let switchCase = t.switchCase(t.numericLiteral(i),  [v.value, t.continueStatement()]);\n            cases.push(switchCase);\n        });\n        let dispenserStr = dispenserArr.join(&#039;|&#039;);\n         \/\/\u751f\u6210_array\u548c_index\u6807\u8bc6\u7b26\uff0c\u5229\u7528BabelAPI\u4fdd\u8bc1\u4e0d\u91cd\u540d\n        let array = path.scope.generateUidIdentifier(&#039;array&#039;);\n        let index = path.scope.generateUidIdentifier(&#039;index&#039;);\n        \/\/\u751f\u6210 &#039;...&#039;.split \u8fd9\u662f\u4e00\u4e2a\u6210\u5458\u8868\u8fbe\u5f0f\n        let callee = t.memberExpression(t.stringLiteral(dispenserStr),  t.identifier(&#039;split&#039;));\n        \/\/\u751f\u6210 split(&#039;|&#039;)\n        let arrayInit = t.callExpression(callee, [t.stringLiteral(&#039;|&#039;)]);\n        \/\/_array\u548c_index\u653e\u5165\u58f0\u660e\u8bed\u53e5\u4e2d\uff0c_index\u521d\u59cb\u5316\u4e3a0\n        let varArray = t.variableDeclarator(array, arrayInit);\n        let varIndex = t.variableDeclarator(index, t.numericLiteral(0));\n        let dispenser = t.variableDeclaration(&#039;let&#039;,[varArray, varIndex]);\n        \/\/\u751f\u6210switch\u4e2d\u7684\u8868\u8fbe\u5f0f +_array[_index++] \n        let updExp = t.updateExpression(&#039;++&#039;, index);\n        let memExp = t.memberExpression(array, updExp, true);\n        let discriminant = t.unaryExpression(&quot;+&quot;, memExp);\n        \/\/\u6784\u5efa\u6574\u4e2aswitch\u8bed\u53e5\n        let switchSta = t.switchStatement(discriminant, cases);\n        \/\/\u751f\u6210while\u5faa\u73af\u4e2d\u7684\u6761\u4ef6 !![]\n        let unaExp = t.unaryExpression(&quot;!&quot;, t.arrayExpression());\n        unaExp = t.unaryExpression(&quot;!&quot;, unaExp);\n        \/\/\u751f\u6210\u6574\u4e2awhile\u5faa\u73af\n        let whileSta = t.whileStatement(unaExp,  t.blockStatement([switchSta, t.breakStatement()]));\n        \/\/\u7528\u5206\u53d1\u5668\u548cwhile\u5faa\u73af\u6765\u6784\u5efablockStatement\uff0c\u66ff\u6362\u539f\u6709\u8282\u70b9\n        path.get(&#039;body&#039;).replaceWith(t.blockStatement([dispenser, whileSta]));\n\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err)=&gt;{});\n\/*\nDate.prototype.format = function (formatStr) {\n  let _array = &quot;0|3|5|1|4|2&quot;.split(&quot;|&quot;),\n    _index = 0;\n  while (!![]) {\n    switch (+_array[_index++]) {\n      case 0:\n        var str = formatStr;\n        continue;\n      case 1:\n        str = str.replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : &#039;0&#039; + (this.getMonth() + 1));\n        continue;\n      case 2:\n        return str;\n        continue;\n      case 3:\n        var Week = [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;];\n        continue;\n      case 4:\n        str = str.replace(\/dd|DD\/, this.getDate() &gt; 9 ? this.getDate().toString() : &#039;0&#039; + this.getDate());\n        continue;\n      case 5:\n        str = str.replace(\/yyyy|YYYY\/, this.getFullYear());\n        continue;\n    }\n    break;\n  }\n};\nconsole.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n*\/<\/code><\/pre>\n<p>\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u505a\u4ee5\u4e0b 3 \u70b9\u8bf4\u660e:<br \/>\n<strong>(1) <\/strong>\u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3,\u4ee3\u7801\u6ca1\u6709\u4f7f\u7528\u5176\u4ed6\u6df7\u6dc6\u65b9\u6848\u3002\u4f46\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d,\u8be5\u6df7\u6dc6\u65b9\u6848\u5fc5\u987b\u548c\u4e4b\u524d\u4ecb\u7ecd\u7684\u5176\u4ed6\u6df7\u6dc6\u65b9\u6848\u914d\u5408\u4f7f\u7528,\u624d\u80fd\u52a0\u5f3a\u6df7\u6dc6\u6548\u679c\u3002\u4f8b\u5982,case \u540e\u9762\u8ddf\u7684\u503c\u53ef\u4ee5\u7528\u6570\u503c\u5e38\u91cf\u52a0\u5bc6(\u5728JS \u4e2d case \u540e\u9762\u53ef\u4ee5\u8ddf\u4e00\u4e2a\u8868\u8fbe\u5f0f,\u800c\u4e0d\u4ec5\u662f\u6570\u503c\u6216\u5b57\u7b26),\u5206\u53d1\u5668\u4e2d\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u5b57\u7b26\u4e32\u52a0\u5bc6,\u7136\u540e\u4e00\u8d77\u63d0\u53d6\u5230\u5927\u6570\u7ec4\u4e2d\u3002\u5bf9\u4e8e\u539f\u59cb\u4ee3\u7801\u90e8\u5206,\u53ef\u4ee5\u8fdb\u884c\u751f\u6210\u82b1\u6307\u4ee4\u9010\u884c\u52a0\u5bc6\u7b49\u5404\u79cd\u64cd\u4f5c\u3002<br \/>\n<strong>(2)<\/strong> switch\u4e2d\u7684\u8868\u8fbe\u5f0f\u4e3a<code>+_array[_index++]<\/code>,\u6700\u524d\u9762\u7684\u52a0\u53f7\u4ee3\u8868\u5f3a\u8f6c\u6210\u6570\u503c\u7c7b\u578b\uff0c\u56e0\u4e3a\u5206\u53d1\u5668\u4e2d\u5b83\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u867d\u7136JS \u662f\u4e00\u95e8\u5f31\u7c7b\u578b\u7684\u8bed\u8a00,\u89e3\u91ca\u5668\u4f1a\u5728\u9002\u5f53\u7684\u65f6\u673a\u5b8c\u6210\u7c7b\u578b\u7684\u81ea\u52a8\u8f6c\u6362,\u4f46\u662f\u5728 case \u4e2d,\u5168\u7b49(===)\u7684\u5339\u914d\u4e0d\u4f1a\u81ea\u52a8\u8f6c\u6362\u7c7b\u578b\u3002<br \/>\n(3)\u7531\u4e8e\u8bed\u53e5\u987a\u5e8f\u88ab\u6253\u4e71,\u56e0\u6b64\u751f\u6210\u7684\u8bed\u53e5\u987a\u5e8f\u6bcf\u4e00\u6b21\u90fd\u662f\u4e0d\u540c\u7684\u3002\u5728\u6d4b\u8bd5\u65f6,\u5e76\u4e0d\u4e00\u5b9a\u4e0e\u4e0a\u8ff0\u987a\u5e8f\u76f8\u540c\u3002<\/p>\n<h3>2.\u5b9e\u73b0\u9017\u53f7\u8868\u8fbe\u5f0f\u6df7\u6dc6<\/h3>\n<p>\u9017\u53f7\u8868\u8fbe\u5f0f\u6df7\u6dc6\u7684\u6838\u5fc3,\u662f\u7528\u9017\u53f7\u8fde\u63a5\u591a\u4e2a\u8868\u8fbe\u5f0f\u3002\u4f46\u662f\u6709\u4e00\u4e9b\u8bed\u53e5\u5728\u8fde\u63a5\u65f6,\u9700\u8981\u8fdb\u884c\u7279\u522b\u5904\u7406\u3002<\/p>\n<h4>1.\u58f0\u660e\u8bed\u53e5\u4e4b\u95f4\u7684\u8fde\u63a5<\/h4>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code class=\"language-javascript\">vara=100;\nvar b = 200;\n\/\/ vara=100,b=200;\n\/\/ vara = 100\uff0cvar b = 200;\u4e0d\u80fd\u8fd9\u6837\u64cd\u4f5c<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u8981\u8fde\u63a5\u4e24\u4e2a\u58f0\u660e\u8bed\u53e5,\u9700\u8981\u53d6\u51fa VariableDeclaration \u4e2d\u7684 declarations\u6570\u7ec4,\u8be5\u6570\u7ec4\u91cc\u662f\u58f0\u660e\u8bed\u53e5\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf,\u7136\u540e\u5c06\u5b83\u5904\u7406\u6210\u4e00\u6761\u58f0\u660e\u8bed\u53e5\u3002\u8fd9\u91cc\u6700\u65b9\u4fbf\u7684\u5904\u7406\u65b9\u5f0f\u662f\u628a\u6240\u6709\u7684\u6807\u8bc6\u7b26\u58f0\u660e\u90fd\u63d0\u53d6\u5230\u53c2\u6570\u4e2d\u3002<\/p>\n<h4>2.\u666e\u901a\u8bed\u53e5\u4e0ereturn \u8bed\u53e5\u8fde\u63a5<\/h4>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">function test(a){\n    a = a+100;\n    return a;\n}\n\/*\n    function test(a){\n        return a = a +100,\n        a;\n        \/\/a = a+100, return a;\u4e0d\u80fd\u8fd9\u6837\u64cd\u4f5c\n    }\n*\/<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa,\u666e\u901a\u8bed\u53e5\u4e0ereturn \u8bed\u53e5\u8fde\u63a5\u65f6,\u9700\u8981\u63d0\u53d6\u51fareturn\u8bed\u53e5\u7684argument\u90e8\u5206\u7136\u540e\u91cd\u65b0\u6784\u5efareturn \u8282\u70b9,\u628a\u6574\u4e2a\u9017\u53f7\u8868\u8fbe\u5f0f\u4f5c\u4e3a argument \u90e8\u5206\u3002\u63a5\u7740\u4ecb\u7ecd\u5982\u4f55\u628a\u51fd\u6570\u4e2d\u6240\u6709\u58f0\u660e\u7684\u53d8\u91cf\u63d0\u53d6\u5230\u53c2\u6570\u5217\u8868\u4e2d,\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">traverse(ast, {\n    FunctionExpression(path){\n        let blockStatement = path.node.body;\n        let blockStatementLength = blockStatement.body.length;\n        if(blockStatementLength &lt; 2) return;\n        \/\/\u628a\u6240\u6709\u58f0\u660e\u7684\u53d8\u91cf\u63d0\u53d6\u5230\u53c2\u6570\u5217\u8868\u4e2d\n        path.traverse({\n            VariableDeclaration(p){\n                declarations = p.node.declarations;\n                let statements = [];\n                declarations.map(function(v){\n                    path.node.params.push(v.id);\n                    v.init &amp;&amp; statements.push(t.assignmentExpression(&#039;=&#039;,v.id,v.init));\n                });\n                p.replaceInline(statements);\n            }\n        });\n    }\n});<\/code><\/pre>\n<p>\u904d\u5386FunctionExpression \u8282\u70b9,\u53d6\u51fa blockStatement,body \u8282\u70b9\uff0c\u8be5\u8282\u70b9\u91cc\u5b58\u653e\u51fd\u6570\u4e2d\u6240\u6709\u7684\u8bed\u53e5\u3002\u5982\u679c\u8bed\u53e5\u5c11\u4e8e\u4e24\u6761,\u5c31\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002\u63a5\u7740,\u904d\u5386\u5f53\u524d\u51fd\u6570\u4e0b\u6240\u6709\u7684VariableDeclaration \u8282\u70b9,\u5176\u4e2d declarations \u6570\u7ec4\u4e3a\u58f0\u660e\u7684\u5177\u4f53\u53d8\u91cf\u3002\u7136\u540e\u901a\u8fc7map \u904d\u5386\u6574\u4e2a\u6570\u7ec4,\u901a\u8fc7 path.node.params.push(v.id)\u5c06\u53d8\u91cf\u63d0\u53d6\u5230\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u4e2d\u3002<\/p>\n<p>\u8981\u8003\u8651\u4e24\u79cd\u60c5\u51b5:<\/p>\n<ul>\n<li>\u53d8\u91cf\u6ca1\u6709\u521d\u59cb\u5316,\u90a3\u4e48\u5b83\u4e0d\u52a0\u5165\u5230 statements \u6570\u7ec4\u4e2d,\u6700\u540e\u66ff\u6362\u8282\u70b9\u65f6\u76f8\u5f53\u4e8e\u88ab\u79fb\u9664\uff1b<\/li>\n<li>\u53d8\u91cf\u521d\u59cb\u5316\uff0c\u90a3\u4e48\u5c31\u5c06 VariableDeclarator \u6539\u4e3a\u8d4b\u503c\u8bed\u53e5\u3002<\/li>\n<\/ul>\n<p>\u6709\u65f6\u5019\u4f1a\u5728\u51fd\u6570\u4f53\u4e2d\u7684\u8bed\u53e5\u5916\u9762\u5305\u88f9\u4e00\u5c42 ExpressionStatement \u8282\u70b9,\u8fd9\u4f1a\u5f71\u54cd\u8bed\u53e5\u7c7b\u578b\u7684\u5224\u65ad\u3002\u6240\u4ee5\u5148\u628a\u8fd9\u7c7b\u8bed\u53e5\u5916\u5c42\u7684 ExpressionStatement \u8282\u70b9\u53bb\u6389,\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code class=\"language-javascript\">let firstSta = blockStatement.body[0],i = 1;\nwhile(i&lt; blockStatementLength){\n    let tempSta = blockStatement.body[i++];\n    t.isExpressionStatement(tempSta) ? secondSta = tempSta.expression : secondSta = tempSta;\n}<\/code><\/pre>\n<p>\u9996\u5148\u53d6\u51fa\u51fd\u6570\u4f53\u4e2d\u7684\u7b2c\u4e00\u6761\u8bed\u53e5,\u5e76\u4e14\u5b9a\u4e49\u4e00\u4e2a\u521d\u59cb\u503c\u4e3a 1\u7684\u8ba1\u6570\u5668 i\u3002\u63a5\u7740\u5bf9\u51fd\u6570\u4f53\u4e2d\u7684\u5176\u4ed6\u8bed\u53e5\u8fdb\u884c\u5faa\u73af\uff0c\u53d6\u51fa\u6765\u7684\u8bed\u53e5\u5148\u8d4b\u503c\u7ed9 tempSta,\u7136\u540e\u5224\u65ad\u662f\u5426\u4e3aExpressionStatement \u8282\u70b9\u3002\u5982\u679c\u662f\uff0c\u5c31\u53d6\u51faexpression \u5c5e\u6027\u8d4b\u503c\u7ed9 secondSta,\u5426\u5219\u76f4\u63a5\u8d4b\u503c\u7ed9 secondSta\u3002<\/p>\n<p>\u7136\u540e\u8981\u628a\u8fd9\u4e24\u4e2a\u8bed\u53e5\u6539\u4e3a\u9017\u53f7\u8868\u8fbe\u5f0f,\u53ef\u4ee5\u4f7f\u7528toSequenceExpression \u6765\u5b8c\u6210\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u8bed\u53e5,\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u5904\u7406\u65b9\u6848\u3002\u672c\u8282\u53ea\u5904\u7406\u5176\u4e2d\u7684\u8d4b\u503c\u8bed\u53e5\u548c\u51fd\u6570\u8c03\u7528\u8bed\u53e5,\u8fd4\u56de\u8bed\u53e5\u4e5f\u9700\u8981\u5904\u7406,\u5982\u4e0b\u6240\u793a:<\/p>\n<pre><code class=\"language-javascript\">\/\/\u5904\u7406\u8fd4\u56de\u8bed\u53e5\nif(t.isReturnStatement(secondSta)){\n    firstSta = t.returnStatement(\n        t.toSequenceExpression([firstSta,secondSta.argument]));\n\/\/\u5904\u7406\u8d4b\u503c\u8bed\u53e5)\n}else if(t.isAssignmentExpression(secondSta)){\n    secondSta.right = t.toSequenceExpression([firstSta, secondSta.right]);\n    firstSta = secondSta;\n}else{\n    firstSta = t.toSequenceExpression([firstSta, secondSta]);\n}<\/code><\/pre>\n<p>\u9996\u5148\u4ecb\u7ecd\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u7684\u8d4b\u503c\u8bed\u53e5\u7684\u5904\u7406\u65b9\u6cd5\u3002\u5224\u65ad\u8bed\u53e5\u5982\u679c\u662f\u8d4b\u503c\u8868\u8fbe\u5f0f,\u5c31\u53d6\u51fa\u5176\u4e2d\u7684right \u8282\u70b9,\u4e0e firstSta \u7ec4\u6210\u9017\u53f7\u8868\u8fbe\u5f0f\u540e,\u518d\u66ff\u6362\u539f\u6709\u7684 right \u8282\u70b9,\u6700\u540e\u628a secondSta\u8d4b\u503c\u7ed9 firstSta,\u8fdb\u884c\u540e\u7eed\u7684\u8bed\u53e5\u5904\u7406\u3002\u63a5\u7740\u4ecb\u7ecd\u8fd4\u56de\u8bed\u53e5,\u53d6\u51fa\u5176\u4e2d\u7684argument \u8282\u70b9,\u4e0efirstSta\u7ec4\u6210\u9017\u53f7\u8868\u8fbe\u5f0f\u540e,\u91cd\u65b0\u751f\u6210\u4e00\u4e2areturnStatement \u8282\u70b9\u3002\u5230\u8fd9\u4e00\u6b65\u5c31\u53ef\u4ee5\u8df3\u51fa\u5faa\u73af,\u56e0\u4e3a\u540e\u7eed\u5373\u4f7f\u518d\u6709\u8bed\u53e5,\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c\u5230\u3002\u5982\u679c\u65e2\u4e0d\u662f\u8fd4\u56de\u8bed\u53e5,\u4e5f\u4e0d\u662f\u8d4b\u503c\u8bed\u53e5,\u90a3\u4e48\u5c31\u76f4\u63a5\u5c06 firstSta \u4e0e secondSta \u7ec4\u6210\u9017\u53f7\u8868\u8fbe\u5f0f,\u8fd9\u662f\u4e00\u79cd\u6700\u6ca1\u6709\u6df7\u6dc6\u529b\u5ea6\u7684\u7ec4\u6210\u65b9\u5f0f\u3002<\/p>\n<p>\u6700\u540e\u518d\u5904\u7406\u51fd\u6570\u8c03\u7528\u8868\u8fbe\u5f0f\u3002\u5728\u539f\u59cb\u4ee3\u7801\u4e2d,str.replace(...)\u53ef\u4ee5\u5904\u7406\u6210(firstSta,str.replace)(...)\u6216\u8005(firstSta,str.replace)(...),\u672c\u8282\u6765\u5b9e\u73b0\u7b2c\u4e00\u79cd\u60c5\u51b5,\u5982\u4e0b\u6240\u793a:<\/p>\n<pre><code class=\"language-javascript\">if(t.isCallExpression(secondSta.right)){\n    let callee = secondSta.right.callee;\n    callee.object = t.toSequenceExpression([firstSta, callee.object]);\n    firstSta = secondSta;\n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d,\u8d4b\u503c\u8bed\u53e5\u7684\u53f3\u8fb9\u662f\u51fd\u6570\u8c03\u7528\u8868\u8fbe\u5f0f\u3002\u53d6\u51fa\u51fd\u6570\u540d callee,str.replace(...)\u7684\u51fd\u6570\u540d\u4e3a str.replace\u3002\u8fd9\u662f\u4e00\u4e2a MemberExpression \u8282\u70b9\u3002\u63a5\u7740\u53d6\u51fa object \u90e8\u5206,\u5373\u5c06str \u4e0e firstSta \u7ec4\u6210\u9017\u53f7\u8868\u8fbe\u5f0f,\u518d\u66ff\u6362\u6389\u539f\u5148\u7684 object \u8282\u70b9\u3002<\/p>\n<p>\u4e0b\u9762\u5c55\u793a\u5b8c\u6574\u7684\u5904\u7406\u4ee3\u7801:<\/p>\n<pre><code class=\"language-javascript\">const parser = require(&quot;@babel\/parser&quot;);\nconst traverse = require(&quot;@babel\/traverse&quot;).default;\nconst t = require(&quot;@babel\/types&quot;);\nconst generator = require(&quot;@babel\/generator&quot;).default;\nconst fs = require(&#039;fs&#039;);\n\nconst jscode = fs.readFileSync(&quot;.\/demo.js&quot;, {\n        encoding: &quot;utf-8&quot;\n    });\nlet ast = parser.parse(jscode);\n\ntraverse(ast, {\n    FunctionExpression(path) {\n        let blockStatement = path.node.body;\n        let blockStatementLength = blockStatement.body.length;\n        if (blockStatementLength &lt; 2) return;\n        \/\/\u628a\u6240\u6709\u58f0\u660e\u7684\u53d8\u91cf\u63d0\u53d6\u5230\u53c2\u6570\u5217\u8868\u4e2d\n        path.traverse({\n            VariableDeclaration(p) {\n                declarations = p.node.declarations;\n                let statements = [];\n                declarations.map(function (v) {\n                    path.node.params.push(v.id);\n                    v.init &amp;&amp; statements.push(t.assignmentExpression(&#039;=&#039;, v.id, v.init));\n                });\n                p.replaceInline(statements);\n            }\n        });\n        \/\/\u5904\u7406\u8d4b\u503c\u8bed\u53e5 \u8fd4\u56de\u8bed\u53e5 \u51fd\u6570\u8c03\u7528\u8bed\u53e5\n        let firstSta = blockStatement.body[0],\n        i = 1;\n        while (i &lt; blockStatementLength) {\n            let tempSta = blockStatement.body[i++];\n            t.isExpressionStatement(tempSta) ?\n            secondSta = tempSta.expression : secondSta = tempSta;\n            \/\/\u5904\u7406\u8fd4\u56de\u8bed\u53e5\n            if (t.isReturnStatement(secondSta)) {\n                firstSta = t.returnStatement(\n                    t.toSequenceExpression([firstSta, secondSta.argument]));\n                \/\/\u5904\u7406\u8d4b\u503c\u8bed\u53e5\n            } else if (t.isAssignmentExpression(secondSta)) {\n                if (t.isCallExpression(secondSta.right)) {\n                    let callee = secondSta.right.callee;\n                    callee.object = t.toSequenceExpression([firstSta, callee.object]);\n                    firstSta = secondSta;\n                } else {\n                    secondSta.right = t.toSequenceExpression([firstSta, secondSta.right]);\n                    firstSta = secondSta;\n                }\n            } else {\n                firstSta = t.toSequenceExpression([firstSta, secondSta]);\n            }\n        }\n        path.get(&#039;body&#039;).replaceWith(t.blockStatement([firstSta]));\n    }\n});\n\nlet code = generator(ast).code;\nfs.writeFile(&#039;.\/demoNew.js&#039;, code, (err) =&gt; {});\n\/*\nDate.prototype.format = function (formatStr, str, Week) {\n  return str = (str = (str = (Week = (str = formatStr, [&#039;\u65e5&#039;, &#039;\u4e00&#039;, &#039;\u4e8c&#039;, &#039;\u4e09&#039;, &#039;\u56db&#039;, &#039;\u4e94&#039;, &#039;\u516d&#039;]), str).replace(\/yyyy|YYYY\/, this.getFullYear()), str).replace(\/MM\/, this.getMonth() + 1 &gt; 9 ? (this.getMonth() + 1).toString() : &#039;0&#039; + (this.getMonth() + 1)), str).replace(\/dd|DD\/, this.getDate() &gt; 9 ? this.getDate().toString() : &#039;0&#039; + this.getDate()), str;\n};\nconsole.log(new Date().format(&#039;yyyy-MM-dd&#039;));\n*\/<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ea\u9002\u7528\u4e8e\u539f\u59cb\u4ee3\u7801\u7684\u6848\u4f8b,\u5b9e\u9645\u5e94\u7528\u4e2d,\u8fd8\u9700\u8981\u8003\u8651\u66f4\u591a\u60c5\u51b5\u3002\u53ef\u4ee5\u770b\u51fa\uff0c\u9017\u53f7\u8868\u8fbe\u5f0f\u6df7\u6dc6\u7684\u5b9e\u73b0\u662f\u6bd4\u8f83\u590d\u6742\u7684,\u4f46\u662f\u9017\u53f7\u8868\u8fbe\u5f0f\u7684\u8fd8\u539f\u5f88\u5bb9\u6613\u5904\u7406\u3002\u540e\u7eed\u7ae0\u8282\u4f1a\u7ed9\u51fa\u76f8\u5e94\u7684\u8fd8\u539f\u65b9\u6848\u3002<\/p>\n<h2>\u516d\u3001\u5c0f\u7ed3<\/h2>\n<p>\u672c\u7ae0\u4ecb\u7ecd\u4e86\u5f88\u591a\u6df7\u6dc6\u7684\u5b9e\u73b0\u65b9\u6848,\u8fd9\u4e9b\u65b9\u6848\u4e4b\u95f4\u9700\u8981\u914d\u5408\u4f7f\u7528\u624d\u80fd\u8fbe\u5230\u66f4\u597d\u7684\u6df7\u6dc6\u6548\u679c\u3002<\/p>\n<p>\u4f46\u662f\u5728\u914d\u5408\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d,\u6709\u4e9b\u65b9\u6848\u9700\u8981\u6ce8\u610f\u987a\u5e8f,\u5982\u4ee3\u7801\u9010\u884c\u52a0\u5bc6,\u7531\u4e8e\u4f1a\u628a\u4ee3\u7801\u4e2d\u7684\u6807\u8bc6\u7b26\u4e00\u8d77\u52a0\u5bc6,\u6240\u4ee5\u9700\u8981\u653e\u5728\u6807\u8bc6\u7b26\u6df7\u6dc6\u4e4b\u540e;\u6709\u4e9b\u65b9\u6848\u4e0d\u9700\u8981\u6ce8\u610f\u987a\u5e8f,\u5982\u6570\u503c\u5e38\u91cf\u6df7\u6dc6\u3002<\/p>\n<p>\u5728\u4ee3\u7801\u6267\u884c\u903b\u8f91\u7684\u6df7\u6dc6\u4e2d,\u6d41\u7a0b\u5e73\u5766\u5316\u7684\u5b9e\u73b0\u65b9\u6848\u4e5f\u6709\u5f88\u591a\u79cd,\u5927\u4f53\u539f\u7406\u5dee\u4e0d\u591a\u3002<\/p>\n<p>\u6b64\u5916,\u672c\u7ae0\u4e0d\u5c11\u5c0f\u8282\u53ea\u5199\u4e86FunctionExpression \u7684\u60c5\u51b5,FunctionDeclaration \u7684\u5904\u7406\u65b9\u6cd5\u4e5f\u662f\u4e00\u6837\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AST\u81ea\u52a8\u5316JavaScript\u9632\u62a4\u65b9\u6848 \u6ca1\u6709\u7279\u522b\u8bf4\u660e\u7684\u60c5\u51b5\u4e0b\uff0c\u90fd\u4ee5\u5904\u7406\u4ee5\u4e0b\u4ee3\u7801\u4e3a\u4f8b\uff1a Date.prototype.format =    \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[194,89],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8889"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8889"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8889\/revisions"}],"predecessor-version":[{"id":8908,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8889\/revisions\/8908"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8889"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}