{"id":2197,"date":"2024-11-10T09:39:13","date_gmt":"2024-11-10T01:39:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2197\/"},"modified":"2024-11-10T09:39:13","modified_gmt":"2024-11-10T01:39:13","slug":"%e7%bc%96%e5%86%99-polyfill-javascript","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2197\/","title":{"rendered":"\u7f16\u5199 polyfill \u2014 Javascript"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173068740367802.jpg\" class=\"aligncenter\" title=\"\u7f16\u5199 polyfill \u2014 Javascript\u63d2\u56fe\" alt=\"\u7f16\u5199 polyfill \u2014 Javascript\u63d2\u56fe\" \/><\/p>\n<p>\u4e00\u6bb5\u4ee3\u7801\uff0c\u63d0\u4f9b\u67d0\u4e9b\u6d4f\u89c8\u5668\u6216\u73af\u5883\u672c\u8eab\u4e0d\u652f\u6301\u7684\u529f\u80fd\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u6d4f\u89c8\u5668\u540e\u5907\u3002<\/p>\n<p>\u5728\u4e3a<strong>call()<\/strong>\u3001<strong>apply()<\/strong>\u548c<strong>bind()<\/strong>\u65b9\u6cd5\u7f16\u5199polyfill\u4e4b\u524d\uff0c\u8bf7\u68c0\u67e5call\u3001apply\u548cbind\u7684\u529f\u80fd\u3002 <\/p>\n<pre>let details = {\n  name: 'manoj',\n  location: 'chennai'\n}\n\nlet getdetails = function (...args) {\n  return `${this.name} from ${this.location}${args.join(', ') ? `, ${args.join(', ')}` : ''}`;\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>1\u3002\u8c03\u7528\u65b9\u5f0f\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u4e3a<strong>call()<\/strong>\u521b\u5efa\u4e00\u4e2apolyfill\u3002\u6211\u4eec\u5c06\u5411 <strong>function.prototype<\/strong> \u6dfb\u52a0\u81ea\u5b9a\u4e49 <strong>call<\/strong> \u65b9\u6cd5\uff0c\u4ee5\u4f7f\u5176\u53ef\u4f9b\u6240\u6709\u51fd\u6570\u8bbf\u95ee\u3002<\/p>\n<pre>getdetails.call(details, 'tamil nadu', 'india');  \/\/ manoj from chennai, tamil nadu, india\n\n\/\/ polyfill\nfunction.prototype.mycall = function (ref, ...args) {\n  if (typeof function.prototype.call === 'function') {  \/\/ checks whether the browser supports call method\n    return this.call(ref, ...args);\n  } else {\n    ref = ref || globalthis;\n    let funcname = math.random();  \/\/ random is used to overwriting a function name\n    while (ref.funcname) {\n      funcname = math.random();\n    }\n    ref[funcname] = this;\n    let result = ref[funcname](...args);\n    delete ref[funcname];\n    return result;\n  }\n}\n\ngetdetails.mycall(details, 'tamil nadu', 'india');  \/\/ manoj from chennai, tamil nadu, india\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2\u3002\u7533\u8bf7\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u4e3a<strong>apply()<\/strong> \u521b\u5efa\u4e00\u4e2apolyfill\u3002\u6211\u4eec\u5c06\u5411 <strong>function.prototype<\/strong> \u6dfb\u52a0\u81ea\u5b9a\u4e49 <strong>apply<\/strong> \u65b9\u6cd5\uff0c\u4ee5\u4f7f\u5176\u53ef\u4f9b\u6240\u6709\u51fd\u6570\u8bbf\u95ee\u3002<\/p>\n<pre>getdetails.apply(details, ['tamil nadu', 'india']);  \/\/ manoj from chennai, tamil nadu, india\n\n\/\/ polyfill\nfunction.prototype.myapply = function (ref, args) {\n  if (typeof function.prototype.apply === 'function') {  \/\/ checks whether the browser supports call method\n    this.apply(ref, args);\n  } else {\n    ref = ref || globalthis;\n    let funcname = math.random();  \/\/ random is to avoid duplication\n    while (ref.funcname) {\n      funcname = math.random();\n    }\n    ref[funcname] = this;\n    let result = ref[funcname](args);\n    delete ref[funcname];\n    return result;\n  }\n}\n\ngetdetails.myapply(details, 'tamil nadu', 'india');  \/\/ manoj from chennai, tamil nadu, india\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3\u3002\u7ed1\u5b9a\u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u8ba9\u6211\u4eec\u4e3a<strong>bind()<\/strong> \u521b\u5efa\u4e00\u4e2apolyfill\u3002\u6211\u4eec\u5c06\u5411 <strong>function.prototype<\/strong> \u6dfb\u52a0\u81ea\u5b9a\u4e49 <strong>bind<\/strong> \u65b9\u6cd5\uff0c\u4ee5\u4f7f\u5176\u53ef\u4f9b\u6240\u6709\u51fd\u6570\u8bbf\u95ee\u3002<\/p>\n<pre>let getFullDetails = getDetails.bind(details, 'Tamil Nadu');\ngetFullDetails();  \/\/ Manoj from Chennai, Tamil Nadu\ngetFullDetails('India');  \/\/ Manoj from Chennai, Tamil Nadu, India\n\n\/\/ Polyfill\nFunction.prototype.myBind = function (ref, ...args) {\n  if (typeof Function.prototype.bind === 'function') {\n    return this.bind(ref, ...args);\n  } else {\n    let fn = this;\n    return function (...args2) {\n        return fn.apply(ref, [...args, ...args2]);  \/\/ Merge and apply arguments\n    }\n  }\n}\n\nlet getFullDetails = getDetails.myBind(details, 'Tamil Nadu');  \/\/ Manoj from Chennai, Tamil Nadu\ngetFullDetails('India');  \/\/ Manoj from Chennai, Tamil Nadu, India\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<p>\u611f\u8c22\u60a8\u7684\u9605\u8bfb\uff01\u6211\u5e0c\u671b\u60a8\u53d1\u73b0\u8fd9\u4e2a\u535a\u5ba2\u4fe1\u606f\u4e30\u5bcc\u4e14\u5f15\u4eba\u5165\u80dc\u3002\u5982\u679c\u60a8\u53d1\u73b0\u4efb\u4f55\u4e0d\u51c6\u786e\u4e4b\u5904\u6216\u6709\u4efb\u4f55\u53cd\u9988\uff0c\u8bf7\u968f\u65f6\u544a\u8bc9\u6211\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7f16\u5199 polyfill \u2014 Javascript\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>\u4e00\u6bb5\u4ee3\u7801\uff0c\u63d0\u4f9b\u67d0\u4e9b\u6d4f\u89c8\u5668\u6216\u73af\u5883\u672c\u8eab\u4e0d\u652f\u6301\u7684\u529f\u80fd\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u6d4f\u89c8\u5668\u540e\u5907\u3002 \u5728\u4e3acall()\u3001apply()\u548cbind()\u65b9\u6cd5\u7f16\u5199polyfill\u4e4b\u524d\uff0c\u8bf7\u68c0\u67e5call\u3001apply\u548cbind\u7684\u529f\u80fd\u3002 let details = { name: &#8216;manoj&#8217;, location: &#8216;chennai&#8217; } let getdetails = function (&#8230;args) { return `${this.name} from ${this.location}${args.join(&#8216;, &#8216;) ? `, ${args.join(&#8216;, &#8216;)}` : &#8221;}`; } \u767b\u5f55\u540e\u590d\u5236 1\u3002\u8c03\u7528\u65b9\u5f0f\uff1a \u8ba9\u6211\u4eec\u4e3acall()\u521b\u5efa\u4e00\u4e2apolyfill\u3002\u6211\u4eec\u5c06\u5411 function.prototype \u6dfb\u52a0\u81ea\u5b9a\u4e49 call \u65b9\u6cd5\uff0c\u4ee5\u4f7f\u5176\u53ef\u4f9b\u6240\u6709\u51fd\u6570\u8bbf\u95ee\u3002 getdetails.call(details, &#8216;tamil nadu&#8217;, &#8216;india&#8217;); \/\/ manoj from chennai, tamil nadu, india \/\/ polyfill function.prototype.mycall = function (ref, &#8230;args) { [&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-2197","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2197","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=2197"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2197\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}