{"id":5174,"date":"2023-01-05T12:07:45","date_gmt":"2023-01-05T04:07:45","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5174"},"modified":"2023-01-05T12:07:45","modified_gmt":"2023-01-05T04:07:45","slug":"%e4%ba%86%e8%a7%a3typescript%e5%87%bd%e6%95%b8functions%e7%9a%84this%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e5%87%bd%e6%95%b8functions%e7%9a%84this%e6%a6%82%e5%bf%b5\/","title":{"rendered":"\u4e86\u89e3TypeScript\u51fd\u6578(functions)\u7684this\u6982\u5ff5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, functions, this\"><\/p>\n<h1>TypeScript \u51fd\u6578(functions)\u7684this<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u6240\u6709\u7684\u529f\u80fd\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u984d\u5916\u7684\u7279\u6027\uff0c\u4f8b\u5982\u985e\u578b\u6aa2\u67e5\u3001\u63a5\u53e3\u3001\u6a21\u7d44\u7b49\u7b49\u3002\u5728 TypeScript \u4e2d\uff0c\u51fd\u6578\u4e5f\u662f\u4e00\u7a2e\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u800c\u51fd\u6578\u4e2d\u7684 this \u4e5f\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u6709\u6548\u7387\u7684\u4f7f\u7528\u51fd\u6578\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u51fd\u6578\u7684 this \u662f\u4e00\u500b\u7279\u6b8a\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u6709\u6548\u7387\u7684\u4f7f\u7528\u51fd\u6578\u3002this \u662f\u4e00\u500b\u6307\u5411\u51fd\u6578\u7684\u7279\u6b8a\u6307\u6a19\uff0c\u5b83\u53ef\u4ee5\u6307\u5411\u51fd\u6578\u7684\u53c3\u6578\u3001\u51fd\u6578\u7684\u5167\u90e8\u8b8a\u6578\u3001\u51fd\u6578\u7684\u5916\u90e8\u8b8a\u6578\u7b49\u7b49\u3002<\/p>\n<h2>this \u7684\u4f7f\u7528<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0cthis \u53ef\u4ee5\u7528\u65bc\u6307\u5411\u51fd\u6578\u7684\u53c3\u6578\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction foo(a: number, b: number) {\n    console.log(this.a + this.b);\n}\n\nlet obj = {\n    a: 1,\n    b: 2\n};\n\nfoo.call(obj, 1, 2); \/\/ 3\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 this \u4f86\u6307\u5411\u51fd\u6578\u7684\u53c3\u6578\uff0c\u4e26\u4e14\u4f7f\u7528 call \u65b9\u6cd5\u4f86\u8abf\u7528\u51fd\u6578\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07 obj \u7684 a \u548c b \u53c3\u6578\u50b3\u905e\u7d66\u51fd\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u51fd\u6578\u4e2d\u4f7f\u7528 this \u4f86\u8a2a\u554f\u53c3\u6578\u3002<\/p>\n<p>\u6b64\u5916\uff0cthis \u9084\u53ef\u4ee5\u7528\u65bc\u6307\u5411\u51fd\u6578\u7684\u5167\u90e8\u8b8a\u6578\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction foo() {\n    let a = 1;\n    let b = 2;\n    console.log(this.a + this.b);\n}\n\nlet obj = {\n    a: 1,\n    b: 2\n};\n\nfoo.call(obj); \/\/ 3\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 this \u4f86\u6307\u5411\u51fd\u6578\u7684\u5167\u90e8\u8b8a\u6578\uff0c\u4e26\u4e14\u4f7f\u7528 call \u65b9\u6cd5\u4f86\u8abf\u7528\u51fd\u6578\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07 obj \u7684 a \u548c b \u53c3\u6578\u50b3\u905e\u7d66\u51fd\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u51fd\u6578\u4e2d\u4f7f\u7528 this \u4f86\u8a2a\u554f\u5167\u90e8\u8b8a\u6578\u3002<\/p>\n<p>\u6700\u5f8c\uff0cthis \u9084\u53ef\u4ee5\u7528\u65bc\u6307\u5411\u51fd\u6578\u7684\u5916\u90e8\u8b8a\u6578\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet a = 1;\nlet b = 2;\n\nfunction foo() {\n    console.log(this.a + this.b);\n}\n\nlet obj = {\n    a: 1,\n    b: 2\n};\n\nfoo.call(obj); \/\/ 3\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 this \u4f86\u6307\u5411\u51fd\u6578\u7684\u5916\u90e8\u8b8a\u6578\uff0c\u4e26\u4e14\u4f7f\u7528 call \u65b9\u6cd5\u4f86\u8abf\u7528\u51fd\u6578\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07 obj \u7684 a \u548c b \u53c3\u6578\u50b3\u905e\u7d66\u51fd\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u51fd\u6578\u4e2d\u4f7f\u7528 this \u4f86\u8a2a\u554f\u5916\u90e8\u8b8a\u6578\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0c\u51fd\u6578\u7684 this \u662f\u4e00\u500b\u7279\u6b8a\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u6709\u6548\u7387\u7684\u4f7f\u7528\u51fd\u6578\u3002this \u53ef\u4ee5\u6307\u5411\u51fd\u6578\u7684\u53c3\u6578\u3001\u51fd\u6578\u7684\u5167\u90e8\u8b8a\u6578\u3001\u51fd\u6578\u7684\u5916\u90e8\u8b8a\u6578\u7b49\u7b49\uff0c\u4f7f\u7528 this \u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u6709\u6548\u7387\u7684\u4f7f\u7528\u51fd\u6578\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3TypeScript\u51fd\u6578(functions)\u7684this\u6982\u5ff5\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528this\u95dc\u9375\u5b57\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u51fd\u6578\u4e2d\u4f7f\u7528this\u4f86\u6307\u5411\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39TypeScript\u51fd\u6578(functions)\u7684this\u6982\u5ff5\uff0c\u8b93\u4f60\u66f4\u52a0\u4e86\u89e3TypeScript\u51fd\u6578(functions)\u7684this\u6982\u5ff5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5174","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1ls","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5174"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5174\/revisions"}],"predecessor-version":[{"id":5175,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5174\/revisions\/5175"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}