{"id":155200,"date":"2024-05-08T11:48:16","date_gmt":"2024-05-08T03:48:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/155200.html"},"modified":"2024-05-08T11:48:19","modified_gmt":"2024-05-08T03:48:19","slug":"%e7%a7%bb%e5%8a%a8%e7%ab%af-javascript-%e5%8a%a8%e6%80%81%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/155200.html","title":{"rendered":"\u79fb\u52a8\u7aef javascript \u52a8\u6001\u51fd\u6570\u5982\u4f55\u521b\u5efa"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26213619\/2dd95c13-c020-4c0a-a9fe-3f61405f2b6b.webp\" alt=\"\u79fb\u52a8\u7aef javascript \u52a8\u6001\u51fd\u6570\u5982\u4f55\u521b\u5efa\" \/><\/p>\n<p><p>\u79fb\u52a8\u7aef JavaScript \u52a8\u6001\u51fd\u6570\u521b\u5efa\u53ef\u4ee5\u901a\u8fc7<strong>\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528Function\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u58f0\u660e\u548c\u4f7f\u7528\u7bad\u5934\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Function\u6784\u9020\u51fd\u6570<\/strong>\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u5b83\u63a5\u53d7\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u53c2\u6570\u548c\u51fd\u6570\u4f53\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u8fd0\u884c\u65f6\u5b9a\u4e49\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u5f0f\u5f88\u9002\u5408\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u4ee3\u7801\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u51fd\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001FUNCTION\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u52a8\u6001\u51fd\u6570\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u662f\u4f7f\u7528JavaScript\u4e2d\u7684Function\u6784\u9020\u51fd\u6570\u3002Function\u6784\u9020\u51fd\u6570\u5141\u8bb8\u4f60\u4f20\u5165\u4e00\u7cfb\u5217\u53c2\u6570\uff0c\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662f\u51fd\u6570\u4f53\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5728\u8fd0\u884c\u65f6\u521b\u5efa\u65b0\u51fd\u6570\uff0c\u4f46\u5b83\u4e5f\u6709\u6027\u80fd\u4e0a\u7684\u7f3a\u70b9\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u89e3\u6790\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Function\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u53c2\u6570\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u53c2\u6570\u5217\u8868\uff0c\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u4e3a\u51fd\u6570\u4f53<\/p>\n<p>var dynamicFunc = new Function(&#039;a&#039;, &#039;b&#039;, &#039;return a + b;&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u52a8\u6001\u51fd\u6570<code>dynamicFunc<\/code>\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570<code>a<\/code>\u548c<code>b<\/code>\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u88ab\u8c03\u7528\u800c\u8868\u73b0\u5f97\u548c\u5e38\u89c4\u51fd\u6570\u4e00\u6837\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var result = dynamicFunc(1, 2);  \/\/ \u7ed3\u679c\u4e3a 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b89\u5168\u6027\u8003\u8651<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Function\u6784\u9020\u51fd\u6570\u65f6\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u5b9e\u9645\u4e0a\u6d89\u53ca\u5230\u4e86\u5b57\u7b26\u4e32\u7684\u89e3\u6790\u548c\u6267\u884c\uff0c\u8fd9\u53ef\u80fd\u5f15\u5165\u5b89\u5168\u98ce\u9669\uff0c\u5c24\u5176\u662f\u5f53\u5b57\u7b26\u4e32\u6765\u6e90\u4e0d\u53ef\u63a7\u65f6\u3002\u5fc5\u987b\u786e\u4fdd\u4f20\u5165Function\u6784\u9020\u51fd\u6570\u7684\u4ee3\u7801\u662f\u5b89\u5168\u7684\uff0c\u4ee5\u9632\u6b62\u4ee3\u7801\u6ce8\u5165\u653b\u51fb\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8d4b\u503c\u58f0\u660e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Function\u6784\u9020\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5c06\u533f\u540d\u51fd\u6570\uff08\u5305\u62ec\u4f20\u7edf\u51fd\u6570\u6216\u7bad\u5934\u51fd\u6570\uff09\u8d4b\u503c\u7ed9\u53d8\u91cf\u6765\u521b\u5efa\u52a8\u6001\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u533f\u540d\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u533f\u540d\u51fd\u6570\u6ca1\u6709\u540d\u79f0\uff0c\u53ef\u4ee5\u5728\u8d4b\u503c\u65f6\u5b9a\u4e49\u5176\u884c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var add = function(a, b) {<\/p>\n<p>  return a + b;<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u521b\u5efa\u7684<code>add<\/code>\u51fd\u6570\u53ef\u4ee5\u968f\u65f6\u6539\u53d8\u5176\u6307\u5411\uff0c\u5b9e\u73b0\u52a8\u6001\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">add = function(a, b) {<\/p>\n<p>  return a * b;<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u91cd\u65b0\u5b9a\u4e49\u4e86<code>add<\/code>\u51fd\u6570\uff0c\u6539\u53d8\u4e86\u5b83\u7684\u884c\u4e3a\uff0c\u4f7f\u5176\u53d8\u6210\u4e86\u4e00\u4e2a\u4e58\u6cd5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u7bad\u5934\u51fd\u6570<\/h4>\n<\/p>\n<p><p>ES6\u4e2d\u5f15\u5165\u7684\u7bad\u5934\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u6765\u521b\u5efa\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var add = (a, b) =&gt; a + b;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u7684\u4e00\u4e2a\u7279\u70b9\u662f\u6ca1\u6709\u81ea\u5df1\u7684<code>this<\/code>\uff0c\u5b83\u4f1a\u6355\u83b7\u5176\u6240\u5728\u4e0a\u4e0b\u6587\u7684<code>this<\/code>\u503c\u3002\u8fd9\u5728\u521b\u5efa\u52a8\u6001\u51fd\u6570\u4e14\u9700\u8981\u4f7f\u7528\u5f53\u524d\u4e0a\u4e0b\u6587\u4e2d\u7684<code>this<\/code>\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528EVAL<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u63a8\u8350\uff0c\u5e76\u4e14\u51fa\u4e8e\u5b89\u5168\u56e0\u7d20\u9700\u8981\u6781\u529b\u907f\u514d\uff0c<code>eval<\/code>\u4e5f\u662fJavaScript\u4e2d\u52a8\u6001\u6267\u884c\u4ee3\u7801\u7684\u4e00\u79cd\u65b9\u5f0f\u3002<code>eval<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u6267\u884c\u5176\u4e2d\u7684JavaScript\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">var code = &#039;var add = function(a, b) { return a + b; };&#039;;<\/p>\n<p>eval(code);<\/p>\n<p>var result = add(1, 2);  \/\/ \u7ed3\u679c\u4e3a 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>eval<\/code>\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u7f16\u8bd1\u548c\u6267\u884c\u4ee3\u7801\uff0c\u5141\u8bb8\u521b\u5efa\u52a8\u6001\u51fd\u6570\u3002\u7136\u800c\uff0c\u7531\u4e8e\u5b89\u5168\u98ce\u9669\u548c\u6027\u80fd\u95ee\u9898\uff0c<code>eval<\/code>\u901a\u5e38\u88ab\u89c6\u4e3a\u4e0d\u597d\u7684\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u5168\u548c\u6027\u80fd<\/h4>\n<\/p>\n<p><p><code>eval<\/code>\u6267\u884c\u7684\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\u5e76\u4fee\u6539\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5982\u679c\u6267\u884c\u7684\u4ee3\u7801\u4e0d\u53d7\u4fe1\u4efb\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b89\u5168\u6f0f\u6d1e\u3002\u540c\u65f6\uff0c<code>eval<\/code>\u901a\u5e38\u6bd4\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5\u6162\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u8fd0\u884c\u65f6\u89e3\u6790\u548c\u7f16\u8bd1\u5b57\u7b26\u4e32\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u5bf9\u8c61\u65b9\u6cd5\u521b\u5efa<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u52a8\u6001\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u968f\u65f6\u66f4\u6539\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u6539\u53d8\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>\u5b9a\u4e49\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5b57\u9762\u91cf\u65f6\u5b9a\u4e49\u65b9\u6cd5\uff0c\u6216\u8005\u5728\u8fd0\u884c\u65f6\u5411\u5bf9\u8c61\u6dfb\u52a0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var calculator = {<\/p>\n<p>  add: function(a, b) {<\/p>\n<p>    return a + b;<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u968f\u540e\uff0c\u4f60\u53ef\u4ee5\u66f4\u6539<code>calculator<\/code>\u5bf9\u8c61\u7684<code>add<\/code>\u65b9\u6cd5\u6765\u6539\u53d8\u5176\u884c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">calculator.add = function(a, b) {<\/p>\n<p>  return a * b;<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5728\u5bf9\u8c61\u4e0a\u52a8\u6001\u521b\u5efa\u548c\u4fee\u6539\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u79fb\u52a8\u7aefJavaScript\u52a8\u6001\u51fd\u6570\u7684\u521b\u5efa\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5e94\u5f53\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u548c\u5b89\u5168\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u5e94\u5f53\u907f\u514d\u4f7f\u7528\u9700\u8981\u89e3\u6790\u5b57\u7b26\u4e32\u4ee3\u7801\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728\u79fb\u52a8\u7aef\u4f7f\u7528 JavaScript \u521b\u5efa\u52a8\u6001\u51fd\u6570\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u4ec0\u4e48\u662f\u52a8\u6001\u51fd\u6570<\/strong>\uff1f<br \/>\n\u52a8\u6001\u51fd\u6570\u662f\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u521b\u5efa\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u5728\u4ee3\u7801\u4e2d\u9759\u6001\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u66f4\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u7a0b\u5e8f\u903b\u8f91\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u79fb\u52a8\u7aef\u5982\u4f55\u521b\u5efa\u52a8\u6001\u51fd\u6570<\/strong>\uff1f<br \/>\n\u5728\u79fb\u52a8\u7aef\uff0c\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 eval() \u51fd\u6570\u6765\u521b\u5efa\u52a8\u6001\u51fd\u6570\u3002eval() \u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u89e3\u91ca\u4e3a JavaScript \u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u5c06\u8981\u521b\u5efa\u7684\u51fd\u6570\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9 eval()\uff0c\u7136\u540e\u5c06\u8fd4\u56de\u7684\u51fd\u6570\u4fdd\u5b58\u5728\u4e00\u4e2a\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5b83\u4eec\u4e4b\u548c\u7684\u52a8\u6001\u51fd\u6570\uff1a<\/p>\n<pre><code>var dynamicFunction = eval(&#039;function add(a, b) { return a + b; }&#039;);\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u4f60\u5c31\u53ef\u4ee5\u50cf\u8c03\u7528\u4efb\u4f55\u5176\u4ed6\u51fd\u6570\u4e00\u6837\u8c03\u7528 dynamicFunction\uff0c\u5e76\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<pre><code>var result = dynamicFunction(2, 3); \/\/ \u7ed3\u679c\u4e3a 5\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><br \/>\n\u4f7f\u7528 eval() \u51fd\u6570\u8981\u8c28\u614e\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6267\u884c\u4efb\u610f\u7684 JavaScript \u4ee3\u7801\uff0c\u5305\u62ec\u6076\u610f\u7684\u4ee3\u7801\u3002\u786e\u4fdd\u4f60\u53ea\u4f20\u9012\u53ef\u4fe1\u4efb\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4e14\u907f\u514d\u5728\u52a8\u6001\u51fd\u6570\u4e2d\u4f7f\u7528 eval()\uff0c\u4ee5\u51cf\u5c11\u5b89\u5168\u98ce\u9669\u3002<\/li>\n<\/ol>\n<p>\u53e6\u5916\uff0c\u52a8\u6001\u51fd\u6570\u7684\u521b\u5efa\u548c\u4f7f\u7528\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u6280\u672f\uff0c\u5982\u95ed\u5305\u6216\u533f\u540d\u51fd\u6570\uff0c\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u79fb\u52a8\u7aef JavaScript \u52a8\u6001\u51fd\u6570\u521b\u5efa\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528Function\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u58f0\u660e\u548c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":155205,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/155200"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=155200"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/155200\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/155205"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=155200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=155200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=155200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}