{"id":3994,"date":"2024-11-10T17:20:50","date_gmt":"2024-11-10T09:20:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3994\/"},"modified":"2024-11-10T17:20:50","modified_gmt":"2024-11-10T09:20:50","slug":"%e5%9c%a8-javascript-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-apply-%e6%96%b9%e6%b3%95%e5%8a%a8%e6%80%81%e6%9b%b4%e6%94%b9-this-%e6%8c%87%e5%90%91%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3994\/","title":{"rendered":"\u5728 JavaScript \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 apply \u65b9\u6cd5\u52a8\u6001\u66f4\u6539 this \u6307\u5411\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173120822685194.jpg\" class=\"aligncenter\" title=\"\u5728 JavaScript \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 apply \u65b9\u6cd5\u52a8\u6001\u66f4\u6539 this \u6307\u5411\uff1f\u63d2\u56fe\" alt=\"\u5728 JavaScript \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 apply \u65b9\u6cd5\u52a8\u6001\u66f4\u6539 this \u6307\u5411\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js \u4e2d\u5173\u4e8e\u66f4\u6539 this \u6307\u5411\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0cthis \u5173\u952e\u5b57\u59cb\u7ec8\u6307\u5411\u8c03\u7528\u65b9\u6cd5\u6216\u51fd\u6570\u5f53\u524d\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u52a8\u6001\u66f4\u6539 this \u7684\u6307\u5411\u3002<\/p>\n<p>\u8003\u8651\u5982\u4e0b\u7684\u9632\u6296\u51fd\u6570\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\n            }, wait)\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8be5\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528\u4e86 apply \u65b9\u6cd5\u3002apply \u6709\u4e24\u4e2a\u4e3b\u8981\u4f5c\u7528\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li> <strong>\u66f4\u6539 this \u6307\u5411\uff1a<\/strong> apply \u53ef\u4ee5\u5c06\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a this \u7684\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u5728\u9632\u6296\u51fd\u6570\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e0a\u4e0b\u6587\u5bf9\u8c61 this\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u4e0a\u4e0b\u6587\u5bf9\u8c61\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/li>\n<li> <strong>\u4f20\u9012\u53c2\u6570\uff1a<\/strong> apply \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5305\u542b\u8981\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\u7684\u6570\u7ec4\u3002\u5728\u9632\u6296\u51fd\u6570\u4e2d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f args\uff0c\u5b83\u5305\u542b\u4e86\u8c03\u7528\u9632\u6296\u51fd\u6570\u65f6\u4f20\u9012\u7684\u6240\u6709\u53c2\u6570\u3002<\/li>\n<\/ol>\n<p>\u56e0\u6b64\uff0c\u5728\u9632\u6296\u51fd\u6570\u4e2d\uff0capply \u65b9\u6cd5\u7528\u4e8e\u66f4\u6539 this \u6307\u5411\u4e3a\u5f53\u524d\u6267\u884c\u4e0a\u4e0b\u6587\u5bf9\u8c61\uff0c\u5e76\u4f20\u9012\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7684\u6240\u6709\u53c2\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728 JavaScript \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 apply \u65b9\u6cd5\u52a8\u6001\u66f4\u6539 this \u6307\u5411\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>js \u4e2d\u5173\u4e8e\u66f4\u6539 this \u6307\u5411\u7684\u95ee\u9898 \u5728 javascript \u4e2d\uff0cthis \u5173\u952e\u5b57\u59cb\u7ec8\u6307\u5411\u8c03\u7528\u65b9\u6cd5\u6216\u51fd\u6570\u5f53\u524d\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u52a8\u6001\u66f4\u6539 this \u7684\u6307\u5411\u3002 \u8003\u8651\u5982\u4e0b\u7684\u9632\u6296\u51fd\u6570\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; }, wait); if (callNow) func.apply(context, args); } else { [&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-3994","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3994","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=3994"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3994\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}