{"id":4800,"date":"2023-01-05T10:22:58","date_gmt":"2023-01-05T02:22:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4800"},"modified":"2023-01-05T10:22:58","modified_gmt":"2023-01-05T02:22:58","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e9%a1%9eclass%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e9%a1%9eclass%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684\u985e(Class)\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u985e(class)\"><\/p>\n<h1>TypeScript \u985e(class)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u7684\u6240\u6709\u529f\u80fd\uff0c\u4e26\u4e14\u64f4\u5145\u4e86\u4e00\u4e9b\u65b0\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u985e(class)\u3002<\/p>\n<p>\u985e(class)\u662f\u4e00\u7a2e\u5c01\u88dd\uff0c\u5b83\u53ef\u4ee5\u5c07\u5c6c\u6027\u548c\u65b9\u6cd5\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u4ee5\u4fbf\u65bc\u7ba1\u7406\u548c\u91cd\u7528\u3002TypeScript \u4e2d\u7684\u985e(class)\u53ef\u4ee5\u4f7f\u7528\u5169\u7a2e\u8a9e\u6cd5\uff1aES5 \u548c ES6\u3002<\/p>\n<h2>ES5 \u985e(class)<\/h2>\n<p>ES5 \u985e(class)\u662f TypeScript \u4e2d\u6700\u7c21\u55ae\u7684\u985e(class)\u8a9e\u6cd5\uff0c\u5b83\u4f7f\u7528 JavaScript \u7684\u51fd\u6578\u4f86\u5b9a\u7fa9\u985e(class)\uff0c\u4e26\u4f7f\u7528 prototype \u5c0d\u8c61\u4f86\u5b9a\u7fa9\u65b9\u6cd5\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684 ES5 \u985e(class)\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nfunction Person(name, age) {\n    this.name = name;\n    this.age = age;\n}\n\nPerson.prototype.sayHello = function() {\n    console.log(\"Hello, my name is \" + this.name);\n};\n\nvar person = new Person(\"John\", 30);\nperson.sayHello(); \/\/ Hello, my name is John\n<\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba Person \u7684\u985e(class)\uff0c\u5b83\u6709\u5169\u500b\u5c6c\u6027\uff1aname \u548c age\uff0c\u4ee5\u53ca\u4e00\u500b\u65b9\u6cd5\uff1asayHello()\u3002<\/p>\n<h2>ES6 \u985e(class)<\/h2>\n<p>ES6 \u985e(class)\u662f TypeScript \u4e2d\u6700\u65b0\u7684\u985e(class)\u8a9e\u6cd5\uff0c\u5b83\u4f7f\u7528 JavaScript \u7684 class \u95dc\u9375\u5b57\u4f86\u5b9a\u7fa9\u985e(class)\uff0c\u4e26\u4f7f\u7528 constructor \u548c methods \u4f86\u5b9a\u7fa9\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684 ES6 \u985e(class)\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass Person {\n    constructor(name, age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    sayHello() {\n        console.log(\"Hello, my name is \" + this.name);\n    }\n}\n\nlet person = new Person(\"John\", 30);\nperson.sayHello(); \/\/ Hello, my name is John\n<\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba Person \u7684\u985e(class)\uff0c\u5b83\u6709\u5169\u500b\u5c6c\u6027\uff1aname \u548c age\uff0c\u4ee5\u53ca\u4e00\u500b\u65b9\u6cd5\uff1asayHello()\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u4e2d\u7684\u985e(class)\u53ef\u4ee5\u4f7f\u7528\u5169\u7a2e\u8a9e\u6cd5\uff1aES5 \u548c ES6\u3002ES5 \u985e(class)\u4f7f\u7528 JavaScript \u7684\u51fd\u6578\u4f86\u5b9a\u7fa9\u985e(class)\uff0c\u4e26\u4f7f\u7528 prototype \u5c0d\u8c61\u4f86\u5b9a\u7fa9\u65b9\u6cd5\uff1b\u800c ES6 \u985e(class)\u5247\u4f7f\u7528 JavaScript \u7684 class \u95dc\u9375\u5b57\u4f86\u5b9a\u7fa9\u985e(class)\uff0c\u4e26\u4f7f\u7528 constructor \u548c methods \u4f86\u5b9a\u7fa9\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p><!--more--><\/p>\n<p>TypeScript \u7684\u985e(class)\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c01\u88dd\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u65bc\u7ba1\u7406\u548c\u91cd\u7528\uff0c\u56e0\u6b64 TypeScript \u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u8a9e\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):TypeScript\u4e2d\u7684\u985e(Class)\u662f\u4e00\u7a2e\u5c01\u88dd\u7269\u4ef6\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c07\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u5c01\u88dd\u5728\u4e00\u8d77\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7a0b\u5f0f\u78bc\u7d50\u69cb\u548c\u66f4\u597d\u7684\u53ef\u8b80\u6027\uff0c\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u4e2d\u7684\u985e(Class)\u529f\u80fd\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-4800","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-1fq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4800","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=4800"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4800\/revisions"}],"predecessor-version":[{"id":4801,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4800\/revisions\/4801"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}