{"id":165796,"date":"2024-05-08T15:23:42","date_gmt":"2024-05-08T07:23:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/165796.html"},"modified":"2024-05-08T15:23:45","modified_gmt":"2024-05-08T07:23:45","slug":"javascript%e4%b8%ad%e5%86%85%e7%bd%aefunction%e5%af%b9%e8%b1%a1%e7%9a%84prototype%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/165796.html","title":{"rendered":"JavaScript\u4e2d\u5185\u7f6eFunction\u5bf9\u8c61\u7684prototype\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26232857\/428dd759-986a-44b4-8b5d-46c50302517a.webp\" alt=\"JavaScript\u4e2d\u5185\u7f6eFunction\u5bf9\u8c61\u7684prototype\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p><strong>JavaScript\u4e2d\u5185\u7f6eFunction\u5bf9\u8c61\u7684prototype\u662fFunction.prototype\uff0c\u5b83\u662f\u6240\u6709JavaScript\u51fd\u6570\u5171\u4eab\u7684\u539f\u578b\u5bf9\u8c61\u3002<\/strong> Function.prototype\u5bf9\u5e94\u7684\u662f\u6240\u6709\u51fd\u6570\u5171\u540c\u7684\u539f\u578b\uff0c\u610f\u5473\u7740\u6240\u6709JavaScript\u4e2d\u521b\u5efa\u7684\u51fd\u6570\u9ed8\u8ba4\u90fd\u4f1a\u7ee7\u627f\u81eaFunction.prototype\u3002\u8fd9\u4e2a\u539f\u578b\u5bf9\u8c61\u81ea\u8eab\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5982apply()\u3001call()\u3001bind()\u7b49\uff0c\u7528\u4ee5\u589e\u5f3a\u51fd\u6570\u7684\u8c03\u7528\u6216\u8005\u6539\u53d8\u51fd\u6570\u7684this\u6307\u5411\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001FUNCTION.PROTOTYPE\u7684\u4f5c\u7528<\/h3>\n<\/p>\n<p><p>Function.prototype\u662f\u6240\u6709\u51fd\u6570\u90fd\u7ee7\u627f\u7684\u539f\u578b\u5bf9\u8c61\uff0c<strong>\u5b83\u63d0\u4f9b\u4e86\u51fd\u6570\u5bf9\u8c61\u5171\u901a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/strong> \u5f53\u4f60\u521b\u5efa\u4e00\u4e2a\u65b0\u51fd\u6570\u65f6\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u4f1a\u81ea\u52a8\u7ee7\u627fFunction.prototype\u4e0a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u662f\u7531JavaScript\u7684\u539f\u578b\u94fe\u673a\u5236\u51b3\u5b9a\u7684\uff0c\u5728\u8fd9\u4e2a\u673a\u5236\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9Function.prototype\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u6240\u6709\u51fd\u6570\u5b9e\u4f8b\u90fd\u80fd\u591f\u8bbf\u95ee\u8fd9\u4e9b\u65b0\u589e\u7684\u6210\u5458\u3002<\/p>\n<\/p>\n<p><h4>\u6269\u5c55Function.prototype<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c55\u793a\u5982\u4f55\u6269\u5c55Function.prototype\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5b83\u4f1a\u5728\u6240\u6709\u51fd\u6570\u5b9e\u4f8b\u4e2d\u53ef\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">Function.prototype.clone = function() {<\/p>\n<p>    var that = this;<\/p>\n<p>    var temp = function temporary() { return that.apply(this, arguments); };<\/p>\n<p>    for(var key in this) {<\/p>\n<p>        if (this.hasOwnProperty(key)) {<\/p>\n<p>            temp[key] = this[key];<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    return temp;<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3aclone\u7684\u65b9\u6cd5\uff0c<strong>\u5141\u8bb8\u521b\u5efa\u4efb\u4f55\u4e00\u4e2a\u51fd\u6570\u7684\u6d45\u526f\u672c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001FUNCTION.PROTOTYPE\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>\u51fd\u6570\u539f\u578b\u5bf9\u8c61Function.prototype\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5c5e\u6027\u5bf9\u6240\u6709\u7684\u51fd\u6570\u90fd\u662f\u53ef\u7528\u7684\uff0c\u56e0\u4e3a\u6240\u6709\u7684\u51fd\u6570\u5bf9\u8c61\u5728\u521b\u5efa\u65f6\u90fd\u4f1a\u81ea\u52a8\u7ee7\u627f\u8fd9\u4e2a\u539f\u578b\u3002<\/p>\n<\/p>\n<p><h4>call()\u3001apply()\u4e0ebind()<\/h4>\n<\/p>\n<ul>\n<li>\n<p>call()<\/p>\n<p>\u6b64\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u80fd\u591f\u4ee5\u4e0d\u540c\u7684this\u503c\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u4ee3\u7801\u3002\u5b83\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u51fd\u6570\u7684\u7075\u6d3b\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>apply()<\/p>\n<p>apply()\u65b9\u6cd5\u8ddfcall()\u975e\u5e38\u76f8\u4f3c\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5b83\u63a5\u6536\u4e00\u4e2a\u6570\u7ec4\u5f62\u5f0f\u7684\u53c2\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>bind()<\/p>\n<p>bind()\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5728bind()\u88ab\u8c03\u7528\u65f6\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u7684this\u88ab\u6307\u5b9a\u4e3abind()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u5176\u4f59\u53c2\u6570\u5c06\u4f5c\u4e3a\u65b0\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f9b\u8c03\u7528\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>toString()\u4e0evalueOf()<\/h4>\n<\/p>\n<ul>\n<li>\n<p>toString()<\/p>\n<p>JavaScript\u51fd\u6570\u7684toString()\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u5f53\u524d\u51fd\u6570\u6e90\u4ee3\u7801\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>valueOf()<\/p>\n<p>valueOf()\u901a\u5e38\u8fd4\u56de\u51fd\u6570\u5bf9\u8c61\u672c\u8eab\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001Function.prototype\u4e0e\u539f\u578b\u94fe<\/h3>\n<\/p>\n<p><p>JavaScript\u4e2d\u5b9e\u73b0\u7ee7\u627f\u7684\u673a\u5236\u662f\u539f\u578b\u94fe\u3002<strong>\u5728\u539f\u578b\u94fe\u673a\u5236\u4e2d\uff0cFunction.prototype\u8d77\u5230\u4e86\u4e00\u4e2a\u6865\u6881\u7684\u4f5c\u7528<\/strong>\uff0c\u8fde\u63a5\u7740\u6784\u9020\u51fd\u6570\u4ee5\u53ca\u5176\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u6784\u9020\u51fd\u6570\u4e0e\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u6240\u6709\u901a\u8fc7new\u5173\u952e\u5b57\u521b\u5efa\u7684\u5bf9\u8c61\u90fd\u4f1a\u6709\u4e00\u4e2a\u6307\u5411\u6784\u9020\u51fd\u6570\u539f\u578b\u5bf9\u8c61\u7684\u5185\u90e8\u94fe\u63a5\u3002\u6784\u9020\u51fd\u6570\u7684prototype\u5c5e\u6027\u6307\u5411\u7684\u5bf9\u8c61\u5c31\u662f\u5b83\u7684\u539f\u578b\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u539f\u578b\u94fe\u7684\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5f53\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\uff0c\u5982\u679c\u5f53\u524d\u5bf9\u8c61\u4e0a\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u4f1a\u67e5\u627e\u4e0e\u5bf9\u8c61\u5173\u8054\u7684\u539f\u578b\u5bf9\u8c61\u3002\u5982\u679c\u8fd8\u662f\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u7ee7\u7eed\u6cbf\u7740\u539f\u578b\u94fe\u67e5\u627e\uff0c\u8fd9\u5c31\u662f\u539f\u578b\u94fe\u7684\u641c\u7d22\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001Function.prototype\u7684\u7279\u6b8a\u6027<\/h3>\n<\/p>\n<p><p>Function.prototype\u5177\u6709\u4e00\u4e9b\u7279\u6b8a\u6027\uff0c\u5b83\u662f\u51fd\u6570\uff0c\u4f46\u53c8\u663e\u793a\u51fa\u4e0e\u5176\u4ed6\u51fd\u6570\u4e0d\u540c\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h4>Function.prototype\u7684\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>Function.prototype\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7a7a\u51fd\u6570\u3002<strong>\u5b83\u662f\u6240\u6709\u51fd\u6570\u4e2d\u552f\u4e00\u6ca1\u6709\u81ea\u5df1\u7684prototype\u5c5e\u6027\u7684\u51fd\u6570<\/strong>\u3002\u5728JavaScript\u4e2d\uff0c\u51fd\u6570\u901a\u5e38\u6709\u81ea\u5df1\u7684prototype\u5c5e\u6027\uff0c\u7528\u4e8e\u5efa\u7acb\u539f\u578b\u94fe\u3002\u4f46\u662fFunction.prototype\u4e0d\u9700\u8981prototype\uff0c\u56e0\u4e3a\u6ca1\u6709\u5176\u4ed6\u5bf9\u8c61\u9700\u8981\u4ece\u5b83\u7ee7\u627f\u5c5e\u6027\u6216\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u4e0d\u5e38\u89c1\u7684\u65b9\u6cd5\u6269\u5c55<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u6211\u4eec\u53ef\u4ee5\u6269\u5c55Function.prototype\uff0c\u4f46\u8fd9\u901a\u5e38\u4e0d\u88ab\u63a8\u8350\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u5168\u5c40\u8303\u56f4\u5185\u6240\u6709\u7684\u51fd\u6570\u5bf9\u8c61\uff0c\u53ef\u80fd\u4f1a\u5f15\u53d1\u610f\u6599\u4e4b\u5916\u7684\u526f\u4f5c\u7528\uff0c\u5c24\u5176\u662f\u5728\u4e0d\u540c\u7684\u5e93\u6216\u8005\u6846\u67b6\u5171\u540c\u5de5\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<strong>Function.prototype\u662f\u6240\u6709Function\u5bf9\u8c61\u5171\u4eab\u7684\u539f\u578b\u5bf9\u8c61<\/strong>\uff0c\u5b83\u63d0\u4f9b\u4e86\u51fd\u6570\u5bf9\u8c61\u5171\u6709\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u540c\u65f6\u4e5f\u5c55\u793a\u4e86JavaScript\u539f\u578b\u94fe\u7ee7\u627f\u7684\u7279\u6027\uff0c\u662f\u7406\u89e3JavaScript\u51fd\u6570\u7ee7\u627f\u673a\u5236\u7684\u5173\u952e\u6240\u5728\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u8bf4JavaScript\u4e2d\u5185\u7f6eFunction\u5bf9\u8c61\u7684prototype\u5f88\u91cd\u8981\uff1f<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0cFunction\u5bf9\u8c61\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5bf9\u8c61\uff0c\u5b83\u662f\u6240\u6709\u51fd\u6570\u7684\u57fa\u7840\u3002\u51fd\u6570\u662fJavaScript\u4e2d\u7684\u4e00\u7b49\u516c\u6c11\uff0c\u62e5\u6709\u7740\u72ec\u7279\u7684\u529f\u80fd\u548c\u7279\u6027\u3002Function\u5bf9\u8c61\u7684prototype\u5c5e\u6027\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5c5e\u6027\u53ef\u4f9b\u7ee7\u627f\u81eaFunction\u5bf9\u8c61\u7684\u6240\u6709\u51fd\u6570\u4f7f\u7528\u3002\u7406\u89e3\u548c\u5229\u7528Function\u5bf9\u8c61\u7684prototype\u5c5e\u6027\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u521b\u5efa\u548c\u4f7f\u7528\u51fd\u6570\u3002<\/p>\n<p><strong>2. Function\u5bf9\u8c61\u7684prototype\u5c5e\u6027\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>Function\u5bf9\u8c61\u7684prototype\u5c5e\u6027\u5305\u542b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7ee7\u627f\u81eaFunction\u5bf9\u8c61\u7684\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<ul>\n<li>call(): \u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u5e76\u5c06\u6307\u5b9a\u7684this\u503c\u548c\u53c2\u6570\u4f20\u9012\u7ed9\u8fd9\u4e2a\u51fd\u6570\u3002<\/li>\n<li>apply(): \u7c7b\u4f3c\u4e8ecall()\u65b9\u6cd5\uff0c\u4f46\u662f\u53c2\u6570\u662f\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u4f20\u9012\u3002<\/li>\n<li>bind(): \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5e76\u5c06\u6307\u5b9a\u7684this\u503c\u548c\u53c2\u6570\u7ed1\u5b9a\u5230\u8fd9\u4e2a\u51fd\u6570\u3002<\/li>\n<li>toString(): \u8fd4\u56de\u51fd\u6570\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/li>\n<li>instanceof: \u68c0\u6d4b\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5b9e\u4f8b\u3002<\/li>\n<li>hasOwnProperty(): \u5224\u65ad\u5bf9\u8c61\u662f\u5426\u5177\u6709\u6307\u5b9a\u7684\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u5728JavaScript\u4e2d\u975e\u5e38\u5e38\u7528\uff0c\u5bf9\u4e8e\u51fd\u6570\u7684\u8c03\u7528\u548c\u5904\u7406\u6765\u8bf4\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u6269\u5c55Function\u5bf9\u8c61\u7684prototype\u5c5e\u6027\uff1f<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e3aFunction\u5bf9\u8c61\u7684prototype\u5c5e\u6027\u6dfb\u52a0\u65b0\u7684\u65b9\u6cd5\u6765\u6269\u5c55\u51fd\u6570\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u6269\u5c55\uff1a<\/p>\n<ol>\n<li>\u83b7\u53d6Function\u5bf9\u8c61\u7684prototype\u5c5e\u6027\uff1a<code>Function.prototype<\/code><\/li>\n<li>\u4e3a\u5176\u6dfb\u52a0\u65b0\u7684\u65b9\u6cd5\u6216\u5c5e\u6027\uff0c\u4f8b\u5982\uff1a<code>Function.prototype.myMethod = function() { ... }<\/code><\/li>\n<li>\u6269\u5c55\u5b8c\u6210\u540e\uff0c\u6240\u6709\u7684\u51fd\u6570\u90fd\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u81eaFunction\u5bf9\u8c61\u7684prototype\u5c5e\u6027\u4f7f\u7528\u8fd9\u4e9b\u65b0\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u6269\u5c55Function\u5bf9\u8c61\u7684prototype\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e9b\u5e38\u7528\u7684\u529f\u80fd\uff0c\u4f7f\u5176\u5728\u6240\u6709\u7684\u51fd\u6570\u4e2d\u90fd\u53ef\u7528\u3002\u8fd9\u79cd\u65b9\u6cd5\u8a00\u7b80\u610f\u8d45\u4e14\u5bb9\u6613\u5b9e\u73b0\uff0c\u662fJavaScript\u4e2d\u975e\u5e38\u5e38\u7528\u7684\u6269\u5c55\u65b9\u5f0f\u4e4b\u4e00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u4e2d\u5185\u7f6eFunction\u5bf9\u8c61\u7684prototype\u662fFunction.prototype\uff0c\u5b83\u662f [&hellip;]","protected":false},"author":3,"featured_media":165801,"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\/165796"}],"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=165796"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/165796\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/165801"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=165796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=165796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=165796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}