{"id":4119,"date":"2024-11-10T12:13:59","date_gmt":"2024-11-10T04:13:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/4119\/"},"modified":"2024-11-10T12:13:59","modified_gmt":"2024-11-10T04:13:59","slug":"javascript-%e4%b8%ad%e7%9a%84-this-%e6%8c%87%e5%90%91%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-apply-%e5%92%8c-call-%e6%96%b9%e6%b3%95%e8%a7%a3%e5%86%b3%e5%87%bd%e6%95%b0%e9%98%b2%e6%8a%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/4119\/","title":{"rendered":"JavaScript \u4e2d\u7684 this \u6307\u5411\uff1a\u5982\u4f55\u4f7f\u7528 apply \u548c call \u65b9\u6cd5\u89e3\u51b3\u51fd\u6570\u9632\u6296\u95ee\u9898\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173122812464004.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684 this \u6307\u5411\uff1a\u5982\u4f55\u4f7f\u7528 apply \u548c call \u65b9\u6cd5\u89e3\u51b3\u51fd\u6570\u9632\u6296\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684 this \u6307\u5411\uff1a\u5982\u4f55\u4f7f\u7528 apply \u548c call \u65b9\u6cd5\u89e3\u51b3\u51fd\u6570\u9632\u6296\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5e94\u5bf9\u66f4\u6539 this \u6307\u5411\u7684\u56f0\u60d1<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0cthis \u6307\u5411\u7ecf\u5e38\u662f\u4e00\u4e2a\u4ee4\u4eba\u56f0\u60d1\u7684\u6982\u5ff5\u3002\u672c\u6587\u5c06\u91cd\u70b9\u63a2\u8ba8 apply \u548c call \u65b9\u6cd5\u5728\u5e94\u5bf9\u6b64\u95ee\u9898\u4e2d\u7684\u5e94\u7528\uff0c\u5e76\u4ee5\u51fd\u6570\u9632\u6296\u4e3a\u4f8b\u8fdb\u884c\u6df1\u5165\u89e3\u6790\u3002<\/p>\n<p><strong>\u51fd\u6570\u9632\u6296\uff1a\u5e94\u7528 apply \u548c call<\/strong><\/p>\n<p>\u51fd\u6570\u9632\u6296\u662f\u4e00\u79cd\u6280\u672f\uff0c\u5b83\u53ef\u4ee5\u907f\u514d\u9891\u7e41\u89e6\u53d1\u4e8b\u4ef6\u56de\u8c03\u3002\u6211\u4eec\u8fd9\u91cc\u63d0\u4f9b\u7684\u9632\u6296\u51fd\u6570\u5229\u7528 apply \u548c call \u65b9\u6cd5\uff1a<\/p>\n<pre>function debounce(func, wait, immediate) {\n\n  let timer;\n  return function() {\n    let context = this,\n        args = arguments;\n    if (timer) clearTimeout(timer);\n    if (immediate) {\n      let callNow = !timer;\n      timer = setTimeout(() =&gt; {\n        timer = null;\n      }, wait);\n      if (callNow) func.apply(context, args);\n    } else {\n      timer  = setTimeout(() =&gt; {\n        func.apply \/* \u2026 *\/; \/* \u7f3a\u5c11\u90e8\u5206\u4ee3\u7801 *\/\n      }, wait)\n    }\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong> apply \u548c call \u7684\u4f5c\u7528<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0capply \u548c call \u65b9\u6cd5\u88ab\u7528\u4e8e\u4ee5\u4e0b\u76ee\u7684\uff1a<\/p>\n<ol>\n<li> <strong>\u8bbe\u7f6e this \u6307\u5411\uff1a<\/strong> apply \u548c call \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a this \u6307\u5411\u7684\u4e0a\u4e0b\u6587\uff0c\u786e\u4fdd\u56de\u8c03\u4ee5\u9002\u5f53\u7684 this \u5bf9\u8c61\u6267\u884c\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e3a\u51fd\u6570\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u3002<\/li>\n<li> <strong>\u4f20\u9012\u53c2\u6570\uff1a<\/strong> apply \u548c call \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5305\u542b\u51fd\u6570\u53c2\u6570\u7684\u6570\u7ec4\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9632\u6296\u51fd\u6570\u5305\u88c5\u5b9e\u9645\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\uff0c\u540c\u65f6\u4ecd\u7136\u80fd\u591f\u63d0\u4f9b\u5fc5\u8981\u7684\u53c2\u6570\u3002<\/li>\n<\/ol>\n<p><strong>\u4e3a\u4f55\u4f7f\u7528 apply \u548c call<\/strong><\/p>\n<p>\u4f7f\u7528 apply \u548c call \u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>\u7075\u6d3b\u6027\uff1a<\/strong> \u5b83\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u51fd\u6570\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u548c\u4f20\u9012\u81ea\u5b9a\u4e49\u53c2\u6570\uff0c\u4ece\u800c\u63d0\u4f9b\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u7075\u6d3b\u6027\u3002<\/li>\n<li> <strong>\u89e3\u51b3 this \u6307\u5411\u95ee\u9898\uff1a<\/strong> apply \u548c call \u786e\u4fdd this \u6307\u5411\u6b63\u786e\u7684\u4e0a\u4e0b\u6587\uff0c\u6709\u52a9\u4e8e\u89e3\u51b3 javascript \u56fa\u6709\u7684 this \u6307\u5411\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u7684 this \u6307\u5411\uff1a\u5982\u4f55\u4f7f\u7528 apply \u548c call \u65b9\u6cd5\u89e3\u51b3\u51fd\u6570\u9632\u6296\u95ee\u9898\uff1f\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>\u5e94\u5bf9\u66f4\u6539 this \u6307\u5411\u7684\u56f0\u60d1 \u5728 javascript \u4e2d\uff0cthis \u6307\u5411\u7ecf\u5e38\u662f\u4e00\u4e2a\u4ee4\u4eba\u56f0\u60d1\u7684\u6982\u5ff5\u3002\u672c\u6587\u5c06\u91cd\u70b9\u63a2\u8ba8 apply \u548c call \u65b9\u6cd5\u5728\u5e94\u5bf9\u6b64\u95ee\u9898\u4e2d\u7684\u5e94\u7528\uff0c\u5e76\u4ee5\u51fd\u6570\u9632\u6296\u4e3a\u4f8b\u8fdb\u884c\u6df1\u5165\u89e3\u6790\u3002 \u51fd\u6570\u9632\u6296\uff1a\u5e94\u7528 apply \u548c call \u51fd\u6570\u9632\u6296\u662f\u4e00\u79cd\u6280\u672f\uff0c\u5b83\u53ef\u4ee5\u907f\u514d\u9891\u7e41\u89e6\u53d1\u4e8b\u4ef6\u56de\u8c03\u3002\u6211\u4eec\u8fd9\u91cc\u63d0\u4f9b\u7684\u9632\u6296\u51fd\u6570\u5229\u7528 apply \u548c call \u65b9\u6cd5\uff1a function debounce(func, wait, immediate) { let timer; return function() { let context = this, args = arguments; if (timer) clearTimeout(timer); if (immediate) { let callNow = !timer; timer = setTimeout(() =&gt; { timer = null; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-4119","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/4119","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=4119"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/4119\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=4119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=4119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=4119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}