{"id":32610,"date":"2024-11-25T13:59:40","date_gmt":"2024-11-25T05:59:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32610\/"},"modified":"2024-11-25T13:59:40","modified_gmt":"2024-11-25T05:59:40","slug":"%e9%87%8d%e6%96%b0%e7%9a%84%e6%a0%87%e9%a2%98%e4%b8%ba%ef%bc%9a%e6%89%a7%e8%a1%8c%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32610\/","title":{"rendered":"\u91cd\u65b0\u7684\u6807\u9898\u4e3a\uff1a\u6267\u884c\u529f\u80fd()"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169375087865650.jpg\" class=\"aligncenter\" title=\"\u91cd\u65b0\u7684\u6807\u9898\u4e3a\uff1a\u6267\u884c\u529f\u80fd()\u63d2\u56fe\" alt=\"\u91cd\u65b0\u7684\u6807\u9898\u4e3a\uff1a\u6267\u884c\u529f\u80fd()\u63d2\u56fe\" \/><\/p>\n<p>\u51fd\u6570\u662f\u4ee3\u7801\u8bed\u53e5\u7684\u5bb9\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7 () \u8fd0\u7b97\u7b26\u8c03\u7528\u3002\u8c03\u7528\u65f6\u53ef\u4ee5\u5728\u62ec\u53f7\u5185\u4f20\u9012\u53c2\u6570\uff0c\u4ee5\u4fbf\u51fd\u6570\u8c03\u7528\u65f6\u51fd\u6570\u4e2d\u7684\u8bed\u53e5\u53ef\u4ee5\u8bbf\u95ee\u67d0\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p>\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 new \u8fd0\u7b97\u7b26\u521b\u5efa addNumbers \u51fd\u6570 objectone \u7684\u4e24\u4e2a\u7248\u672c\uff0c\u53e6\u4e00\u4e2a\u7248\u672c\u4f7f\u7528\u66f4\u5e38\u89c1\u7684\u6587\u5b57\u6a21\u5f0f\u3002\u4e24\u8005\u90fd\u9700\u8981\u4e24\u4e2a\u53c2\u6570\u3002\u5728\u6bcf\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u90fd\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5e76\u5728\u62ec\u53f7\u4e2d\u4f20\u9012\u53c2\u6570 () \u8fd0\u7b97\u7b26\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample76.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar addNumbersA = new Function('num1', 'num2', 'return num1 + num2');\n\n\tconsole.log(addNumbersA(2, 2)); \/\/ Logs 4.\n\n\t\/\/ Could also be written the literal way, which is much more common.\n\tvar addNumbersB = function (num1, num2) { return num1 + num2; };\n\n\tconsole.log(addNumbersB(2, 2)); \/\/ Logs 4.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u51fd\u6570\u53ef\u7528\u4e8e\u8fd4\u56de\u503c\u3001\u6784\u9020\u5bf9\u8c61\u6216\u4f5c\u4e3a\u7b80\u5355\u8fd0\u884c\u4ee3\u7801\u7684\u673a\u5236\u3002 JavaScript \u5bf9\u51fd\u6570\u6709\u591a\u79cd\u7528\u9014\uff0c\u4f46\u5c31\u5176\u6700\u57fa\u672c\u7684\u5f62\u5f0f\u800c\u8a00\uff0c\u51fd\u6570\u53ea\u662f\u53ef\u6267\u884c\u8bed\u53e5\u7684\u552f\u4e00\u8303\u56f4\u3002<\/p>\n<hr>\n<h2> \u51fd\u6570() \u53c2\u6570<\/h2>\n<p>Function() \u6784\u9020\u51fd\u6570\u91c7\u7528\u65e0\u9650\u6570\u91cf\u7684\u53c2\u6570\uff0c\u4f46 Function() \u6784\u9020\u51fd\u6570\u671f\u671b\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u6784\u6210\u51fd\u6570\u4f53\u7684\u8bed\u53e5\u3002\u5728\u6700\u540e\u4e00\u4e2a\u4e4b\u524d\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570\u7684\u4efb\u4f55\u53c2\u6570\u90fd\u53ef\u7528\u4e8e\u6b63\u5728\u521b\u5efa\u7684\u51fd\u6570\u3002\u8fd8\u53ef\u4ee5\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u53d1\u9001\u591a\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u5c06 Function() \u6784\u9020\u51fd\u6570\u7684\u7528\u6cd5\u4e0e\u5b9e\u4f8b\u5316\u51fd\u6570\u5bf9\u8c61\u7684\u66f4\u5e38\u89c1\u6a21\u5f0f\u8fdb\u884c\u4e86\u5bf9\u6bd4\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample77.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar addFunction = new Function('num1', 'num2', 'return num1 + num2');\n\n\t\/* Alternately, a single comma-separated string with arguments can be \n\tthe first parameter of the constructor, with the function body following. *\/\n\tvar timesFunction = new Function('num1,num2', 'return num1 * num2');\n\n\tconsole.log(addFunction(2, 2), timesFunction(2, 2)); \/\/ Logs '4 4'\n\n\t\/\/ Versus the more common patterns for instantiating a function:\n\tvar addFunction = function (num1, num2) { return num1 + num2; }; \/\/ Expression form.\n\tfunction addFunction(num1, num2) { return num1 + num2; } \/\/ Statement form.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0d\u5efa\u8bae\u6216\u901a\u5e38\u4e0d\u76f4\u63a5\u5229\u7528 Function() \u6784\u9020\u51fd\u6570\uff0c\u56e0\u4e3a JavaScript \u5c06\u4f7f\u7528 eval() \u6765\u89e3\u6790\u5305\u542b\u51fd\u6570\u903b\u8f91\u7684\u5b57\u7b26\u4e32\u3002\u8bb8\u591a\u4eba\u8ba4\u4e3a eval() \u662f\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\u3002\u5982\u679c\u4f7f\u7528\u5b83\uff0c\u5219\u4ee3\u7801\u8bbe\u8ba1\u4e2d\u5f88\u53ef\u80fd\u5b58\u5728\u7f3a\u9677\u3002<\/p>\n<p>\u4f7f\u7528 Function() \u6784\u9020\u51fd\u6570\u800c\u4e0d\u4f7f\u7528 new \u5173\u952e\u5b57\u4e0e\u4ec5\u4f7f\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u51fd\u6570\u5bf9\u8c61\u5177\u6709\u76f8\u540c\u7684\u6548\u679c\uff08new Function(&#8216;x&#8217;,&#8217;return x&#8217;) \u4e0e \u51fd\u6570((&#8216;x&#8217;,&#8217;\u8fd4\u56dex&#8217;))\u3002<\/p>\n<p>\u76f4\u63a5\u8c03\u7528 Function() \u6784\u9020\u51fd\u6570\u65f6\u4e0d\u4f1a\u521b\u5efa\u95ed\u5305\u3002<\/p>\n<hr>\n<h2> Function() \u5c5e\u6027\u548c\u65b9\u6cd5<\/h2>\n<p>\u51fd\u6570\u5bf9\u8c61\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\uff08\u4e0d\u5305\u62ec\u7ee7\u627f\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\uff1a<\/p>\n<p>\u5c5e\u6027\uff08Function.prototype;\uff09\uff1a<\/p>\n<ul>\n<li>\u539f\u578b<\/li>\n<\/ul>\n<hr>\n<h2>\u51fd\u6570\u5bf9\u8c61\u5b9e\u4f8b\u5c5e\u6027\u548c\u65b9\u6cd5<\/h2>\n<p>\u51fd\u6570\u5bf9\u8c61\u5b9e\u4f8b\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\u548c\u65b9\u6cd5\uff08\u4e0d\u5305\u62ec\u7ee7\u627f\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\uff1a<\/p>\n<p>\u5b9e\u4f8b\u5c5e\u6027 (var myFunction = function(x, y, z) {}; myFunction.length;):<\/p>\n<ul>\n<li>\u53c2\u6570<\/li>\n<li>\u6784\u9020\u51fd\u6570<\/li>\n<li>\u957f\u5ea6<\/li>\n<\/ul>\n<p>\u5b9e\u4f8b\u65b9\u6cd5 (var myFunction = function(x, y, z) {}; myFunction.toString();): <\/p>\n<ul>\n<li>apply()<\/li>\n<li>call()<\/li>\n<li>toString()<\/li>\n<\/ul>\n<hr>\n<h2>\u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u503c<\/h2>\n<p>\u867d\u7136\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u7b80\u5355\u5730\u6267\u884c\u4ee3\u7801\u8bed\u53e5\uff0c\u4f46\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u503c\u4e5f\u5f88\u5e38\u89c1\u3002\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4ece sayHi \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample78.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar sayHi = function () {\n\t\treturn 'Hi';\n\t};\n\n\tconsole.log(sayHi()); \/\/ Logs \"Hi\".\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u51fd\u6570\u6ca1\u6709\u6307\u5b9a\u8fd4\u56de\u503c\uff0c\u5219\u8fd4\u56de undefined\u3002\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8c03\u7528 yelp \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u201cyelp\u201d\u8bb0\u5f55\u5230\u63a7\u5236\u53f0\uff0c\u800c\u4e0d\u663e\u5f0f\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample79.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar yelp = function () {\n\t\tconsole.log('I am yelping!');\n\t\t\/\/ Functions return undefined even if we don't.\n\t}\n\n\t\/* Logs true because a value is always returned, even if we don't specifically return one. *\/\n\tconsole.log(yelp() === undefined);\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u91cc\u8981\u8bb0\u4f4f\u7684\u6982\u5ff5\u662f\uff0c\u5373\u4f7f\u60a8\u6ca1\u6709\u663e\u5f0f\u63d0\u4f9b\u8981\u8fd4\u56de\u7684\u503c\uff0c\u6240\u6709\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u8fd4\u56de\u503c\uff0c\u5219\u8fd4\u56de\u503c\u4e3a undefined\u3002<\/p>\n<hr>\n<h2>\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff08\u4e0d\u4ec5\u4ec5\u662f\u8bed\u6cd5\uff0c\u8fd8\u6709\u503c\uff09<\/h2>\n<p>\u5728 JavaScript \u4e2d\uff0c\u51fd\u6570\u5c31\u662f\u5bf9\u8c61\u3002\u8fd9\u610f\u5473\u7740\u51fd\u6570\u53ef\u4ee5\u5b58\u50a8\u5728\u53d8\u91cf\u3001\u6570\u7ec4\u6216\u5bf9\u8c61\u4e2d\u3002\u6b64\u5916\uff0c\u51fd\u6570\u53ef\u4ee5\u4f20\u9012\u7ed9\u51fd\u6570\u6216\u4ece\u51fd\u6570\u8fd4\u56de\u3002\u51fd\u6570\u5177\u6709\u5c5e\u6027\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\u6240\u6709\u8fd9\u4e9b\u56e0\u7d20\u4f7f\u5f97\u51fd\u6570\u6210\u4e3a JavaScript \u4e2d\u7684\u4e00\u7b49\u516c\u6c11\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample80.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Functions can be stored in variables (funcA), arrays (funcB), and objects (funcC).\n\tvar funcA = function () { }; \/\/ Called like so: funcA()\n\tvar funcB = [function () { } ]; \/\/ Called like so: funcB[0]()\n\tvar funcC = { method: function () { } }; \/\/ too.method() or funcC['method']()\n\n\t\/\/ Functions can be sent to and sent back from functions.\n\tvar funcD = function (func) {\n\t\treturn func\n\t};\n\n\tvar runFuncPassedToFuncD = funcD(function () { console.log('Hi'); });\n\n\trunFuncPassedToFuncD();\n\n\t\/\/ Functions are objects, which means they can have properties.\n\tvar funcE = function () { };\n\tfuncE.answer = 'yup'; \/\/ Instance property.\n\tconsole.log(funcE.answer); \/\/ Logs 'yup'.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8ba4\u8bc6\u5230\u51fd\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u662f\u4e00\u4e2a\u503c\uff0c\u8fd9\u4e00\u70b9\u81f3\u5173\u91cd\u8981\u3002\u5b83\u53ef\u4ee5\u50cf JavaScript \u4e2d\u7684\u4efb\u4f55\u5176\u4ed6\u8868\u8fbe\u5f0f\u4e00\u6837\u4f20\u9012\u6216\u589e\u5f3a\u3002<\/p>\n<hr>\n<h2>\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570<\/h2>\n<p>\u53c2\u6570\u662f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5c06\u503c\u4f20\u9012\u5230\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5de5\u5177\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8c03\u7528 addFunction()\u3002\u7531\u4e8e\u6211\u4eec\u5df2\u9884\u5b9a\u4e49\u5b83\u91c7\u7528\u4e24\u4e2a\u53c2\u6570\uff0c\u56e0\u6b64\u5728\u5176\u8303\u56f4\u5185\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u9644\u52a0\u503c\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample81.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar addFunction = function (number1, number2) {\n\t\tvar sum = number1 + number2;\n\t\treturn sum;\n\t}\n\n\tconsole.log(addFunction(3, 3)); \/\/ Logs 6.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0e\u5176\u4ed6\u4e00\u4e9b\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4\uff0c\u5728 JavaScript \u4e2d\u7701\u7565\u53c2\u6570\u662f\u5b8c\u5168\u5408\u6cd5\u7684\uff0c\u5373\u4f7f\u51fd\u6570\u5df2\u88ab\u5b9a\u4e49\u4e3a\u63a5\u53d7\u8fd9\u4e9b\u53c2\u6570\u3002\u7f3a\u5c11\u7684\u53c2\u6570\u4ec5\u88ab\u8d4b\u4e88\u503c undefined\u3002\u5f53\u7136\uff0c\u5982\u679c\u7701\u7565\u53c2\u6570\u503c\uff0c\u8be5\u51fd\u6570\u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u5411\u51fd\u6570\u4f20\u9012\u610f\u5916\u53c2\u6570\uff08\u521b\u5efa\u51fd\u6570\u65f6\u672a\u5b9a\u4e49\u7684\u53c2\u6570\uff09\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u9519\u8bef\u3002\u5e76\u4e14\u53ef\u4ee5\u4ece arguments \u5bf9\u8c61\u8bbf\u95ee\u8fd9\u4e9b\u53c2\u6570\uff0c\u8be5\u5bf9\u8c61\u53ef\u7528\u4e8e\u6240\u6709\u51fd\u6570\u3002<\/p>\n<hr>\n<h2> this \u548c arguments \u503c\u53ef\u7528\u4e8e\u6240\u6709\u51fd\u6570<\/h2>\n<p>\u5728\u6240\u6709\u51fd\u6570\u7684\u8303\u56f4\u548c\u4e3b\u4f53\u5185\uff0cthis \u548c arguments \u503c\u53ef\u7528\u3002<\/p>\n<p>arguments \u5bf9\u8c61\u662f\u4e00\u4e2a\u7c7b\u4f3c\u6570\u7ec4\u7684\u5bf9\u8c61\uff0c\u5305\u542b\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6240\u6709\u53c2\u6570\u3002\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5373\u4f7f\u6211\u4eec\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u653e\u5f03\u6307\u5b9a\u53c2\u6570\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f9d\u8d56\u4f20\u9012\u7ed9\u51fd\u6570\u7684 arguments \u6570\u7ec4\u6765\u8bbf\u95ee\u5728\u8c03\u7528\u65f6\u53d1\u9001\u7684\u53c2\u6570\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample82.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar add = function () {\n\t\treturn arguments[0] + arguments[1];\n\t};\n\n\tconsole.log(add(4, 4)); \/\/ Returns 8.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>this \u5173\u952e\u5b57\uff0c\u4f20\u9012\u7ed9\u6240\u6709\u51fd\u6570\uff0c\u662f\u5bf9\u5305\u542b\u8be5\u51fd\u6570\u7684\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6b63\u5982\u60a8\u6240\u671f\u671b\u7684\uff0c\u5bf9\u8c61\u4e2d\u5305\u542b\u7684\u4f5c\u4e3a\u5c5e\u6027\uff08\u65b9\u6cd5\uff09\u7684\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528 this \u6765\u83b7\u53d6\u5bf9\u7236\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u5f53\u51fd\u6570\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df\u65f6\uff0cthis \u7684\u503c\u4e3a\u5168\u5c40\u5bf9\u8c61\u3002\u67e5\u770b\u4ee5\u4e0b\u4ee3\u7801\u5e76\u786e\u4fdd\u60a8\u4e86\u89e3 this \u8fd4\u56de\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample83.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar myObject1 = {\n\t\tname: 'myObject1',\n\t\tmyMethod: function () { console.log(this); }\n\t};\n\n\tmyObject1.myMethod(); \/\/ Logs 'myObject1'.\n\n\tvar myObject2 = function () { console.log(this); };\n\n\tmyObject2(); \/\/ Logs window.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2>arguments.callee \u5c5e\u6027<\/h2>\n<p>arguments \u5bf9\u8c61\u6709\u4e00\u4e2a\u540d\u4e3a callee \u7684\u5c5e\u6027\uff0c\u5b83\u662f\u5bf9\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u51fd\u6570\u7684\u5f15\u7528\u3002\u6b64\u5c5e\u6027\u53ef\u7528\u4e8e\u4ece\u51fd\u6570\u8303\u56f4\u5185\u5f15\u7528\u8be5\u51fd\u6570 (arguments.callee)a \u81ea\u5f15\u7528\u3002\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b64\u5c5e\u6027\u6765\u83b7\u53d6\u5bf9\u8c03\u7528\u51fd\u6570\u7684\u5f15\u7528\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample84.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar foo = function foo() {\n\t\tconsole.log(arguments.callee); \/\/ Logs foo()\n\t\t\/\/ callee could be used to invoke recursively the foo function (arguments.callee())\n\t} ();\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5f53\u9700\u8981\u9012\u5f52\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u8fd9\u975e\u5e38\u6709\u7528\u3002<\/p>\n<hr>\n<h2>\u51fd\u6570\u5b9e\u4f8b length \u5c5e\u6027\u548c arguments.length <\/h2>\n<p>arguments \u5bf9\u8c61\u5177\u6709\u552f\u4e00\u7684 length \u5c5e\u6027\u3002\u867d\u7136\u60a8\u53ef\u80fd\u8ba4\u4e3a\u8fd9\u4e2a length \u5c5e\u6027\u5c06\u4e3a\u60a8\u63d0\u4f9b\u5df2\u5b9a\u4e49\u53c2\u6570\u7684\u6570\u91cf\uff0c\u4f46\u5b83\u5b9e\u9645\u4e0a\u63d0\u4f9b\u4e86\u5728\u8c03\u7528\u671f\u95f4\u53d1\u9001\u5230\u51fd\u6570\u7684\u53c2\u6570\u6570\u91cf\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample85.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar myFunction = function (z, s, d) {\n\t\treturn arguments.length;\n\t};\n\n\tconsole.log(myFunction()); \/\/ Logs 0 because no parameters were passed to the function.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528\u6240\u6709 Function() \u5b9e\u4f8b\u7684 length \u5c5e\u6027\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u53ef\u4ee5\u83b7\u53d6\u51fd\u6570\u671f\u671b\u7684\u53c2\u6570\u603b\u6570\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample86.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar myFunction = function (z, s, d, e, r, m, q) {\n\t\treturn myFunction.length;\n\t};\n\n\tconsole.log(myFunction()); \/\/ Logs 7.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>arguments.length \u5c5e\u6027\u5728 JavaScript 1.4 \u4e2d\u5df2\u5f03\u7528\uff0c\u4f46\u53ef\u4ee5\u4ece\u51fd\u6570\u5bf9\u8c61\u7684 length \u5c5e\u6027\u8bbf\u95ee\u53d1\u9001\u5230\u51fd\u6570\u7684\u53c2\u6570\u6570\u91cf\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5229\u7528 callee \u5c5e\u6027\u6765\u9996\u5148\u83b7\u53d6\u5bf9\u6b63\u5728\u8c03\u7528\u7684\u51fd\u6570\u7684\u5f15\u7528 (arguments.callee.length) \u6765\u83b7\u53d6\u957f\u5ea6\u503c\u3002<\/p>\n<hr>\n<h2>\u91cd\u65b0\u5b9a\u4e49\u51fd\u6570\u53c2\u6570<\/h2>\n<p>\u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u91cd\u65b0\u5b9a\u4e49\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 arguments \u6570\u7ec4\u3002\u770b\u4e00\u4e0b\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<p>\u793a\u4f8b\uff1asample87.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar foo = false;\n\tvar bar = false;\n\n\tvar myFunction = function (foo, bar) {\n\t\targuments[0] = true;\n\t\tbar = true;\n\t\tconsole.log(arguments[0], bar); \/\/ Logs true true.\n\t}\n\n\tmyFunction();\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6211\u53ef\u4ee5\u4f7f\u7528 arguments \u7d22\u5f15\u6216\u76f4\u63a5\u4e3a\u53c2\u6570\u91cd\u65b0\u5206\u914d\u65b0\u503c\u6765\u91cd\u65b0\u5b9a\u4e49 bar \u53c2\u6570\u7684\u503c\u3002<\/p>\n<hr>\n<h2>\u5728\u51fd\u6570\u5b8c\u6210\u4e4b\u524d\u8fd4\u56de\u51fd\u6570\uff08\u53d6\u6d88\u51fd\u6570\u6267\u884c\uff09<\/h2>\n<p>\u901a\u8fc7\u4f7f\u7528\u5e26\u6216\u4e0d\u5e26\u503c\u7684 return \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u671f\u95f4\u968f\u65f6\u53d6\u6d88\u51fd\u6570\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u53c2\u6570\u672a\u5b9a\u4e49\u6216\u4e0d\u662f\u6570\u5b57\uff0c\u6211\u4eec\u5c06\u53d6\u6d88 add \u51fd\u6570\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample88.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar add = function (x, y) {\n\t\t\/\/ If the parameters are not numbers, return error.\n\t\tif (typeof x !== 'number' || typeof y !== 'number') { return 'pass in numbers'; }\n\t\treturn x + y;\n\t}\n\tconsole.log(add(3, 3)); \/\/ Logs 6.\n\tconsole.log(add('2', '2')); \/\/ Logs 'pass in numbers'.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u91cc\u8981\u8bb2\u7684\u6982\u5ff5\u662f\uff0c\u60a8\u53ef\u4ee5\u5728\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4efb\u4f55\u65f6\u523b\u4f7f\u7528 return \u5173\u952e\u5b57\u6765\u53d6\u6d88\u51fd\u6570\u7684\u6267\u884c\u3002<\/p>\n<hr>\n<h2>\u5b9a\u4e49\u51fd\u6570\uff08\u8bed\u53e5\u3001\u8868\u8fbe\u5f0f\u6216\u6784\u9020\u51fd\u6570\uff09<\/h2>\n<p>\u51fd\u6570\u53ef\u4ee5\u7528\u4e09\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9a\u4e49\uff1a\u51fd\u6570\u6784\u9020\u51fd\u6570\u3001\u51fd\u6570\u8bed\u53e5\u6216\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u6f14\u793a\u4e86\u6bcf\u79cd\u53d8\u4f53\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample89.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/* Function constructor: The last parameter is the function logic, \n\teverything before it is a parameter. *\/\n\tvar addConstructor = new Function('x', 'y', 'return x + y');\n\n\t\/\/ Function statement.\n\tfunction addStatement(x, y) {\n\t\treturn x + y;\n\t}\n\n\t\/\/ Function expression.\n\tvar addExpression = function (x, y) {\n\t\treturn x + y;\n\t};\n\n\tconsole.log(addConstructor(2, 2), addStatement(2, 2), addExpression(2, 2)); \/\/ Logs '4 4 4'.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6709\u4eba\u8bf4\u51fd\u6570\u8fd8\u6709\u7b2c\u56db\u79cd\u7c7b\u578b\u7684\u5b9a\u4e49\uff0c\u79f0\u4e3a\u201c\u547d\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u201d\u3002\u547d\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\u53ea\u662f\u4e00\u4e2a\u5305\u542b\u540d\u79f0\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\uff08\u4f8b\u5982\uff0c var add = function add(x, y) {return x+y}\uff09\u3002<\/p>\n<hr>\n<h2>\u8c03\u7528\u51fd\u6570\uff08\u51fd\u6570\u3001\u65b9\u6cd5\u3001\u6784\u9020\u51fd\u6570\u6216 call() \u548c apply())<\/h2>\n<p>\u4f7f\u7528\u56db\u79cd\u4e0d\u540c\u7684\u573a\u666f\u6216\u6a21\u5f0f\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<ul>\n<li>\u4f5c\u4e3a\u51fd\u6570<\/li>\n<li>\u4f5c\u4e3a\u4e00\u79cd\u65b9\u6cd5<\/li>\n<li>\u4f5c\u4e3a\u6784\u9020\u51fd\u6570<\/li>\n<li>\u4f7f\u7528 apply() \u6216 call() <\/li>\n<\/ul>\n<p>\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u68c0\u67e5\u6bcf\u79cd\u8c03\u7528\u6a21\u5f0f\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample90.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Function pattern.\n\tvar myFunction = function () { return 'foo' };\n\tconsole.log(myFunction()); \/\/ Logs 'foo'.\n\n\t\/\/ Method pattern.\n\tvar myObject = { myFunction: function () { return 'bar'; } }\n\tconsole.log(myObject.myFunction()); \/\/ Logs 'bar'.\n\n\t\/\/ Constructor pattern.\n\tvar Cody = function () {\n\t\tthis.living = true;\n\t\tthis.age = 33;\n\t\tthis.gender = 'male';\n\t\tthis.getGender = function () { return this.gender; };\n\t}\n\tvar cody = new Cody(); \/\/ Invoke via the Cody constructor.\n\tconsole.log(cody); \/\/ Logs the cody object and properties.\n\n\t\/\/ apply() and call() pattern.\n\tvar greet = {\n\t\trunGreet: function () {\n\t\t\tconsole.log(this.name, arguments[0], arguments[1]);\n\t\t}\n\t}\n\n\tvar cody = { name: 'cody' };\n\tvar lisa = { name: 'lisa' };\n\n\t\/\/ Invoke the runGreet function as if it were inside of the cody object.\n\tgreet.runGreet.call(cody, 'foo', 'bar'); \/\/ Logs 'cody foo bar'.\n\n\t\/\/ Invoke the runGreet function as if it were inside of the lisa object.\n\tgreet.runGreet.apply(lisa, ['foo', 'bar']); \/\/ Logs 'lisa foo bar'.\n\n\t\/* Notice the difference between call() and apply() in how parameters are sent to the function being invoked. *\/\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u786e\u4fdd\u60a8\u4e86\u89e3\u6240\u6709\u56db\u79cd\u8c03\u7528\u6a21\u5f0f\uff0c\u56e0\u4e3a\u60a8\u5c06\u9047\u5230\u7684\u4ee3\u7801\u53ef\u80fd\u5305\u542b\u5176\u4e2d\u4efb\u4f55\u4e00\u79cd\u3002<\/p>\n<hr>\n<h2>\u533f\u540d\u51fd\u6570<\/h2>\n<p>\u533f\u540d\u51fd\u6570\u662f\u6ca1\u6709\u7ed9\u51fa\u6807\u8bc6\u7b26\u7684\u51fd\u6570\u3002\u533f\u540d\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample91.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ function(){console.log('hi');}; \/\/ Anonymous function, but no way to invoke it.\n\n\t\/\/ Create a function that can invoke our anonymous function.\n\tvar sayHi = function (f) {\n\t\tf(); \/\/ Invoke the anonymous function.\n\t}\n\n\t\/\/ Pass an anonymous function as a parameter.\n\tsayHi(function () { console.log('hi'); }); \/\/ Logs 'hi'.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2>\u81ea\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f<\/h2>\n<p>\u51fd\u6570\u8868\u8fbe\u5f0f\uff08\u5b9e\u9645\u4e0a\u662f\u9664\u4ece Function() \u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u51fd\u6570\u4e4b\u5916\u7684\u4efb\u4f55\u51fd\u6570\uff09\u53ef\u4ee5\u5728\u5b9a\u4e49\u540e\u4f7f\u7528\u62ec\u53f7\u8fd0\u7b97\u7b26\u7acb\u5373\u8c03\u7528\u3002\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa sayWord() \u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u7acb\u5373\u8c03\u7528\u8be5\u51fd\u6570\u3002\u8fd9\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u81ea\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample92.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar sayWord = function () { console.log('Word 2 yo mo!'); } (); \/\/ Logs 'Word 2 yo mo!'\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2>\u81ea\u8c03\u7528\u533f\u540d\u51fd\u6570\u8bed\u53e5<\/h2>\n<p>\u53ef\u4ee5\u521b\u5efa\u81ea\u8c03\u7528\u7684\u533f\u540d\u51fd\u6570\u8bed\u53e5\u3002\u8fd9\u79f0\u4e3a\u81ea\u8c03\u7528\u533f\u540d\u51fd\u6570\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u51e0\u4e2a\u7acb\u5373\u8c03\u7528\u7684\u533f\u540d\u51fd\u6570\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample93.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Most commonly used\/seen in the wild.\n\t(function (msg) {\n\t\tconsole.log(msg);\n\t})('Hi');\n\n\t\/\/ Slightly different, but achieving the same thing:\n\t(function (msg) {\n\t\tconsole.log(msg)\n\t} ('Hi'));\n\n\t\/\/ The shortest possible solution.\n\t!function sayHi(msg) { console.log(msg); } ('Hi');\n\n\t\/\/ FYI, this does NOT work!\n\t\/\/ function sayHi() {console.log('hi');}();\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6839\u636e ECMAScript \u6807\u51c6\uff0c\u5982\u679c\u8981\u7acb\u5373\u8c03\u7528\u51fd\u6570\uff0c\u5219\u9700\u8981\u5728\u51fd\u6570\u4e24\u8fb9\u52a0\u4e0a\u62ec\u53f7\uff08\u6216\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u8868\u8fbe\u5f0f\u7684\u4efb\u4f55\u5185\u5bb9\uff09\u3002<\/p>\n<hr>\n<h2>\u51fd\u6570\u53ef\u4ee5\u5d4c\u5957<\/h2>\n<p>\u51fd\u6570\u53ef\u4ee5\u65e0\u9650\u671f\u5730\u5d4c\u5957\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u3002\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06 goo \u51fd\u6570\u5c01\u88c5\u5728 bar \u51fd\u6570\u5185\u90e8\uff0c\u8be5\u51fd\u6570\u4f4d\u4e8e foo \u51fd\u6570\u5185\u90e8\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample94.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar foo = function () {\n\t\tvar bar = function () {\n\t\t\tvar goo = function () {\n\t\t\t\tconsole.log(this); \/\/ Logs reference to head window object.\n\t\t\t} ();\n\t\t} ();\n\t} ();\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u91cc\u7684\u7b80\u5355\u6982\u5ff5\u662f\u51fd\u6570\u53ef\u4ee5\u5d4c\u5957\uff0c\u5e76\u4e14\u5d4c\u5957\u7684\u6df1\u5ea6\u6ca1\u6709\u9650\u5236\u3002 <\/p>\n<p>\u8bf7\u8bb0\u4f4f\uff0c\u5d4c\u5957\u51fd\u6570\u7684 this \u7684\u503c\u5c06\u662f JavaScript 1.5\u3001ECMA-262 \u7b2c 3 \u7248\u4e2d\u7684\u5934\u5bf9\u8c61\uff08Web \u6d4f\u89c8\u5668\u4e2d\u7684 window \u5bf9\u8c61\uff09\u3002<\/p>\n<hr>\n<h2>\u5c06\u51fd\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u4ee5\u53ca\u4ece\u51fd\u6570\u8fd4\u56de\u51fd\u6570<\/h2>\n<p>\u5982\u524d\u6240\u8ff0\uff0c\u51fd\u6570\u662f JavaScript \u4e2d\u7684\u4e00\u7b49\u516c\u6c11\u3002\u7531\u4e8e\u51fd\u6570\u662f\u4e00\u4e2a\u503c\uff0c\u5e76\u4e14\u51fd\u6570\u53ef\u4ee5\u4f20\u9012\u4efb\u4f55\u7c7b\u578b\u7684\u503c\uff0c\u56e0\u6b64\u51fd\u6570\u53ef\u4ee5\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u63a5\u53d7\u548c\/\u6216\u8fd4\u56de\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570\u6709\u65f6\u79f0\u4e3a\u201c\u9ad8\u9636\u51fd\u6570\u201d\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u4f20\u9012\u7ed9 foo \u51fd\u6570\uff0c\u7136\u540e\u7acb\u5373\u4ece foo \u51fd\u6570\u8fd4\u56de\u3002\u53d8\u91cf bar \u6240\u6307\u5411\u7684\u6b63\u662f\u8fd9\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u56e0\u4e3a foo \u63a5\u53d7\u5e76\u8fd4\u56de\u533f\u540d\u51fd\u6570\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample95.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Functions can be sent to, and sent back from, functions.\n\tvar foo = function (f) {\n\t\treturn f;\n\t}\n\n\tvar bar = foo(function () { console.log('Hi'); });\n\n\tbar(); \/\/ Logs 'Hi'.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56e0\u6b64\uff0c\u5f53\u8c03\u7528 bar \u65f6\uff0c\u5b83\u4f1a\u8c03\u7528\u4f20\u9012\u7ed9 foo() \u51fd\u6570\u7684\u533f\u540d\u51fd\u6570\uff0c\u7136\u540e\u4ece foo() \u51fd\u6570\u4f20\u56de\u5e76\u4ece bar \u5f15\u7528\u591a\u53d8\u7684\u3002\u6240\u6709\u8fd9\u4e9b\u90fd\u662f\u4e3a\u4e86\u5c55\u793a\u51fd\u6570\u53ef\u4ee5\u50cf\u4efb\u4f55\u5176\u4ed6\u503c\u4e00\u6837\u4f20\u9012\u7684\u4e8b\u5b9e\u3002<\/p>\n<hr>\n<h2>\u5728\u5b9a\u4e49\u51fd\u6570\u8bed\u53e5\u4e4b\u524d\u8c03\u7528\u51fd\u6570\u8bed\u53e5\uff08\u53c8\u540d\u51fd\u6570\u63d0\u5347\uff09<\/h2>\n<p>\u51fd\u6570\u8bed\u53e5\u53ef\u4ee5\u5728\u6267\u884c\u671f\u95f4\u5728\u5176\u5b9e\u9645\u5b9a\u4e49\u4e4b\u524d\u8c03\u7528\u3002\u8fd9\u6709\u70b9\u5947\u602a\uff0c\u4f46\u60a8\u5e94\u8be5\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\uff0c\u4ee5\u4fbf\u60a8\u53ef\u4ee5\u5229\u7528\u5b83\uff0c\u6216\u8005\u81f3\u5c11\u77e5\u9053\u5f53\u60a8\u9047\u5230\u5b83\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5728\u5b9a\u4e49 sayYo() \u548c sum() \u51fd\u6570\u8bed\u53e5\u4e4b\u524d\u8c03\u7528\u5b83\u4eec\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample96.html<\/p>\n<pre>\n&lt;script&gt;\n\n\t\/\/ Example 1\n\tvar speak = function () {\n\t\tsayYo(); \/\/ sayYo() has not been defined yet, but it can still be invoked, logs 'yo'.\n\t\tfunction sayYo() { console.log('Yo'); }\n\t} (); \/\/ Invoke\n\n\t\/\/ Example 2\n\tconsole.log(sum(2, 2)); \/\/ Invoke sum(), which is not defined yet, but can still be invoked.\n\tfunction sum(x, y) { return x + y; }\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u662f\u56e0\u4e3a\u5728\u4ee3\u7801\u8fd0\u884c\u4e4b\u524d\uff0c\u51fd\u6570\u8bed\u53e5\u88ab\u89e3\u91ca\u5e76\u6dfb\u52a0\u5230\u6267\u884c\u5806\u6808\/\u4e0a\u4e0b\u6587\u4e2d\u3002\u786e\u4fdd\u60a8\u5728\u4f7f\u7528\u51fd\u6570\u8bed\u53e5\u65f6\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\u3002<\/p>\n<p>\u5b9a\u4e49\u4e3a\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u51fd\u6570\u4e0d\u4f1a\u88ab\u63d0\u5347\u3002\u4ec5\u63d0\u5347\u51fd\u6570\u8bed\u53e5\u3002<\/p>\n<hr>\n<h2>\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u81ea\u8eab\uff08\u53c8\u540d\u9012\u5f52\uff09<\/h2>\n<p>\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u662f\u5b8c\u5168\u5408\u6cd5\u7684\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u7ecf\u5e38\u88ab\u7528\u5728\u4f17\u6240\u5468\u77e5\u7684\u7f16\u7801\u6a21\u5f0f\u4e2d\u3002\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u542f\u52a8 countDownFrom \u51fd\u6570\uff0c\u7136\u540e\u8be5\u51fd\u6570\u901a\u8fc7\u51fd\u6570\u540d\u79f0 countDownFrom \u8c03\u7528\u81ea\u8eab\u3002\u672c\u8d28\u4e0a\uff0c\u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u4ece 5 \u5012\u6570\u5230 0 \u7684\u5faa\u73af\u3002<\/p>\n<p>\u793a\u4f8b\uff1asample97.html<\/p>\n<pre>\n&lt;script&gt;\n\n\tvar countDownFrom = function countDownFrom(num) {\n\t\tconsole.log(num);\n\t\tnum--; \/\/ Change the parameter value.\n\t\tif (num &lt; 0) { return false; } \/\/ If num &lt; 0 return function with no recursion.\n\t\t\/\/ Could have also done arguments.callee(num) if it was an anonymous function.\n\t\tcountDownFrom(num);\n\t};\n\n\tcountDownFrom(5); \/\/ Kick off the function, which logs separately 5, 4, 3, 2, 1, 0.\n\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u60a8\u5e94\u8be5\u610f\u8bc6\u5230\uff0c\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff08\u4e5f\u79f0\u4e3a\u9012\u5f52\uff09\u6216\u91cd\u590d\u6267\u884c\u6b64\u64cd\u4f5c\u662f\u5f88\u81ea\u7136\u7684\u3002<\/p>\n<hr>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u51fd\u6570\u662f JavaScript \u6700\u5e38\u7528\u7684\u65b9\u9762\u4e4b\u4e00\uff0c\u5e0c\u671b\u60a8\u73b0\u5728\u5bf9\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6709\u4e86\u66f4\u597d\u7684\u4e86\u89e3\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u91cd\u65b0\u7684\u6807\u9898\u4e3a\uff1a\u6267\u884c\u529f\u80fd()\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u662f\u4ee3\u7801\u8bed\u53e5\u7684\u5bb9\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7 () \u8fd0\u7b97\u7b26\u8c03\u7528\u3002\u8c03\u7528\u65f6\u53ef\u4ee5\u5728\u62ec\u53f7\u5185\u4f20\u9012\u53c2\u6570\uff0c\u4ee5\u4fbf\u51fd\u6570\u8c03\u7528\u65f6\u51fd\u6570\u4e2d\u7684\u8bed\u53e5\u53ef\u4ee5\u8bbf\u95ee\u67d0\u4e9b\u503c\u3002 \u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 new \u8fd0\u7b97\u7b26\u521b\u5efa addNumbers \u51fd\u6570 objectone \u7684\u4e24\u4e2a\u7248\u672c\uff0c\u53e6\u4e00\u4e2a\u7248\u672c\u4f7f\u7528\u66f4\u5e38\u89c1\u7684\u6587\u5b57\u6a21\u5f0f\u3002\u4e24\u8005\u90fd\u9700\u8981\u4e24\u4e2a\u53c2\u6570\u3002\u5728\u6bcf\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u90fd\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5e76\u5728\u62ec\u53f7\u4e2d\u4f20\u9012\u53c2\u6570 () \u8fd0\u7b97\u7b26\u3002 \u793a\u4f8b\uff1asample76.html &lt;script&gt; var addNumbersA = new Function(&#8216;num1&#8217;, &#8216;num2&#8217;, &#8216;return num1 + num2&#8217;); console.log(addNumbersA(2, 2)); \/\/ Logs 4. \/\/ Could also be written the literal way, which is much more common. var addNumbersB = function (num1, num2) { return num1 + num2; }; console.log(addNumbersB(2, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32610","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32610"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32610\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}