{"id":2008,"date":"2025-10-14T19:25:08","date_gmt":"2025-10-14T11:25:08","guid":{"rendered":"https:\/\/womeifei.cn\/?p=2008"},"modified":"2025-10-14T19:25:08","modified_gmt":"2025-10-14T11:25:08","slug":"%e6%89%8b%e5%86%99call%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/womeifei.cn\/index.php\/2025\/10\/14\/%e6%89%8b%e5%86%99call%e5%87%bd%e6%95%b0\/","title":{"rendered":"\u624b\u5199call\u51fd\u6570"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u524d\u7f6e\u77e5\u8bc6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.Symbol\u6570\u636e\u7c7b\u578b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Symbol<\/code>\u00a0\u7684\u503c\u5177\u6709\u552f\u4e00\u6027\uff0c\u53ef\u907f\u514d\u5728\u5bf9\u8c61\u4e0a\u6302\u8f7d\u51fd\u6570\u65f6\u8986\u76d6\u539f\u6709\u5c5e\u6027\uff08\u89e3\u51b3 &#8221; \u6c61\u67d3\u00a0<code>context<\/code>&#8221; \u95ee\u9898\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.Object.defineProperty\u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u4e8e\u76f4\u63a5\u5728\u5bf9\u8c61\u4e0a\u5b9a\u4e49\u65b0\u5c5e\u6027\u6216\u4fee\u6539\u73b0\u6709\u5c5e\u6027\uff0c\u5e76\u7cbe\u786e\u63a7\u5236\u8fd9\u4e9b\u5c5e\u6027\u7684\u884c\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Object.defineProperty(obj, prop, descriptor)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.\u4fee\u6539this\u6307\u5411<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>myCall<\/code>\u9700\u8981\u8ba9\u51fd\u6570\u5728\u8c03\u7528\u65f6\uff0c<code>this<\/code>\u6307\u5411\u6211\u4eec\u6307\u5b9a\u7684 <code>ctx<\/code>\u5bf9\u8c61<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u552f\u4e00\u65b9\u6cd5\u5c31\u662f<strong>\u5c06\u51fd\u6570\u4f5c\u4e3a ctx\u7684\u65b9\u6cd5\u8c03\u7528<\/strong>\u200b\uff08\u5373 <code>ctx.method()<\/code>\u7684\u5f62\u5f0f\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5bf9\u4e0a\u4e0b\u6587\u5904\u7406<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ctx = (ctx === null || ctx === undefined) ? globalThis : Object(ctx);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6821\u9a8c\u8c03\u7528\u8005<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>if (typeof this !== 'function') {\n        throw new TypeError('Function.prototype.myCall called on non-function');\n    }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6e05\u7406\u5c5e\u6027<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>delete ctx&#91;key];<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e0d\u8981\u5fd8\u4e86\u8fd4\u56de\u503c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const res = ctx&#91;key](...args)\nreturn res<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u7ec8\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Function.prototype.myCall = function (ctx, ...args) {\n    \/\/ \u7279\u6b8a\u60c5\u51b5\u5904\u7406\u4e0a\u4e0b\u6587\n    ctx = (ctx === null || ctx === undefined) ? globalThis : Object(ctx);\n\n    \/\/ \u6821\u9a8c\u8c03\u7528\u8005\n    if (typeof this !== 'function') {\n        throw new TypeError('Function.prototype.myCall called on non-function');\n    }\n\n    \/\/ \u521b\u5efa\u552f\u4e00key \n    \/\/ \u907f\u514d\u5728\u5bf9\u8c61\u4e0a\u6302\u8f7d\u51fd\u6570\u65f6\u8986\u76d6\u539f\u6709\u5c5e\u6027\n    const key = Symbol('temp');\n\n    \/\/ \u5b9a\u4e49\u4e34\u65f6\u65b9\u6cd5\n    \/\/ ctx&#91;key] = this\u76f4\u63a5\u8d4b\u503c\u521b\u5efa\u7684\u5c5e\u6027\u9ed8\u8ba4\u662f\u53ef\u679a\u4e3e\u7684\n    \/\/ \u4fee\u6539\u4e86\u51fd\u6570\u8c03\u7528\u65f6this\u7684\u6307\u5411\n    Object.defineProperty(ctx, key, {\n        enumerable: false,\n        \/\/ \u8bbe\u4e3a\u4e0d\u53ef\u679a\u4e3e\uff0c\u907f\u514d\u6c61\u67d3\u5bf9\u8c61\u7684\u5c5e\u6027\u679a\u4e3e\u7ed3\u679c\n        value: this\n    });\n\n    \/\/ \u6267\u884c\u5e76\u4fdd\u5b58\u7ed3\u679c\n    const res = ctx&#91;key](...args);\n\n    \/\/ \u6e05\u7406\n    delete ctx&#91;key];\n\n    return res;\n}\nfunction test(a, b) {\n    console.log(this, a, b);\n    return a + b;\n}\n\n\/\/ \u57fa\u672c\u6d4b\u8bd5\nconsole.log(test.myCall({ x: 1 }, 2, 3));\n\/\/ { x: 1 } 2 3\n\/\/ 5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7f6e\u77e5\u8bc6 1.Symbol\u6570\u636e\u7c7b\u578b Symbol\u00a0\u7684\u503c\u5177\u6709\u552f\u4e00\u6027\uff0c\u53ef\u907f\u514d\u5728\u5bf9\u8c61\u4e0a\u6302\u8f7d\u51fd\u6570\u65f6\u8986\u76d6\u539f\u6709\u5c5e\u6027\uff08\u89e3\u51b3  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,1,46],"tags":[],"class_list":["post-2008","post","type-post","status-publish","format-standard","hentry","category-javascript","category-1","category-46"],"_links":{"self":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2008"}],"version-history":[{"count":4,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2008\/revisions"}],"predecessor-version":[{"id":2012,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2008\/revisions\/2012"}],"wp:attachment":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}