{"id":4978,"date":"2023-01-05T11:32:09","date_gmt":"2023-01-05T03:32:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4978"},"modified":"2023-01-05T11:32:09","modified_gmt":"2023-01-05T03:32:09","slug":"%e6%b7%b1%e5%85%a5%e6%8e%a2%e7%b4%a2typescript%e4%b8%ad%e7%9a%84%e9%a0%82%e7%b4%9athis%e9%97%9c%e9%8d%b5%e5%ad%97","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e6%b7%b1%e5%85%a5%e6%8e%a2%e7%b4%a2typescript%e4%b8%ad%e7%9a%84%e9%a0%82%e7%b4%9athis%e9%97%9c%e9%8d%b5%e5%ad%97\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22TypeScript\u4e2d\u7684\u9802\u7d1athis\u95dc\u9375\u5b57"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, this \u95dc\u9375\u5b57, Top-level this Keyword\"><\/p>\n<h1>TypeScript \u4e2d\u7684\u9802\u7d1a this \u95dc\u9375\u5b57<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u975c\u614b\u985e\u578b\u6aa2\u67e5\u3001\u6a21\u584a\u7cfb\u7d71\u3001\u985e\u578b\u5b89\u5168\u7b49\u7b49\u3002\u5176\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\u5c31\u662f\u300c\u9802\u7d1a this \u95dc\u9375\u5b57\u300d\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728 TypeScript \u4e2d\u66f4\u5bb9\u6613\u5730\u4f7f\u7528 this \u95dc\u9375\u5b57\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0cthis \u95dc\u9375\u5b57\u7684\u6307\u5411\u662f\u52d5\u614b\u7684\uff0c\u5b83\u53d6\u6c7a\u65bc\u51fd\u6578\u8abf\u7528\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u500b\u51fd\u6578\u662f\u4f5c\u70ba\u4e00\u500b\u7269\u4ef6\u7684\u65b9\u6cd5\u8abf\u7528\u7684\uff0c\u90a3\u9ebc this \u95dc\u9375\u5b57\u5c31\u6703\u6307\u5411\u8a72\u7269\u4ef6\uff1b\u5982\u679c\u4e00\u500b\u51fd\u6578\u662f\u4f5c\u70ba\u4e00\u500b\u5168\u5c40\u51fd\u6578\u8abf\u7528\u7684\uff0c\u90a3\u9ebc this \u95dc\u9375\u5b57\u5c31\u6703\u6307\u5411\u5168\u5c40\u5c0d\u8c61\uff08window \u5c0d\u8c61\uff09\u3002<\/p>\n<h2>TypeScript \u4e2d\u7684\u9802\u7d1a this \u95dc\u9375\u5b57<\/h2>\n<p>TypeScript \u4e2d\u7684\u9802\u7d1a this \u95dc\u9375\u5b57\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u4f7f\u7528 this \u95dc\u9375\u5b57\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u51fd\u6578\u4e2d\u4f7f\u7528 this \u95dc\u9375\u5b57\uff0c\u800c\u4e0d\u7528\u64d4\u5fc3 this \u95dc\u9375\u5b57\u6307\u5411\u7684\u5c0d\u8c61\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba Person \u7684\u985e\uff0c\u5b83\u6709\u4e00\u500b\u540d\u70ba sayHello \u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass Person {\n  name: string;\n  age: number;\n\n  constructor(name: string, age: number) {\n    this.name = name;\n    this.age = age;\n  }\n\n  sayHello() {\n    console.log(`Hello, my name is <span class=\"katex math inline\">{this.name} and I am<\/span>{this.age} years old.`);\n  }\n}\n<\/pre>\n<p>\u5728 JavaScript \u4e2d\uff0c\u5982\u679c\u6211\u5011\u60f3\u8981\u5728 sayHello \u65b9\u6cd5\u4e2d\u4f7f\u7528 this \u95dc\u9375\u5b57\uff0c\u6211\u5011\u5fc5\u9808\u78ba\u4fdd\u5b83\u662f\u4f5c\u70ba\u4e00\u500b\u7269\u4ef6\u7684\u65b9\u6cd5\u8abf\u7528\u7684\uff0c\u5426\u5247 this \u95dc\u9375\u5b57\u5c31\u6703\u6307\u5411\u5168\u5c40\u5c0d\u8c61\uff0c\u5c0e\u81f4\u7a0b\u5e8f\u51fa\u932f\u3002<\/p>\n<p>\u4f46\u662f\u5728 TypeScript \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u9802\u7d1a this \u95dc\u9375\u5b57\u4f86\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass Person {\n  name: string;\n  age: number;\n\n  constructor(name: string, age: number) {\n    this.name = name;\n    this.age = age;\n  }\n\n  sayHello() {\n    console.log(`Hello, my name is <span class=\"katex math inline\">{this.name} and I am<\/span>{this.age} years old.`);\n  }\n}\n\nlet person = new Person(\"John\", 30);\nperson.sayHello(); \/\/ Hello, my name is John and I am 30 years old.\n<\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86\u9802\u7d1a this \u95dc\u9375\u5b57\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5728 sayHello \u65b9\u6cd5\u4e2d\u4f7f\u7528 this \u95dc\u9375\u5b57\uff0c\u800c\u4e0d\u7528\u64d4\u5fc3 this \u95dc\u9375\u5b57\u6307\u5411\u7684\u5c0d\u8c61\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u4e2d\u7684\u9802\u7d1a this \u95dc\u9375\u5b57\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u4f7f\u7528 this \u95dc\u9375\u5b57\uff0c\u800c\u4e0d\u7528\u64d4\u5fc3 this \u95dc\u9375\u5b57\u6307\u5411\u7684\u5c0d\u8c61\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u51fd\u6578\u4e2d\u4f7f\u7528 this \u95dc\u9375\u5b57\uff0c\u800c\u4e0d\u7528\u64d4\u5fc3 this \u95dc\u9375\u5b57\u6307\u5411\u7684\u5c0d\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eTypeScript\u4e2d\u7684\u9802\u7d1athis\u95dc\u9375\u5b57\uff0c\u8a73\u7d30\u4ecb\u7d39\u5b83\u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4f7f\u7528\u5b83\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-4978","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-1ii","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4978","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=4978"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4978\/revisions"}],"predecessor-version":[{"id":4979,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4978\/revisions\/4979"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}