{"id":2100,"date":"2025-02-27T23:42:06","date_gmt":"2025-02-27T15:42:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2100"},"modified":"2025-02-27T23:57:10","modified_gmt":"2025-02-27T15:57:10","slug":"javascript-%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95-static-methods","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/27\/javascript-%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95-static-methods\/","title":{"rendered":"JavaScript \u9759\u6001\u65b9\u6cd5 (Static Methods)"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c\u9759\u6001\u65b9\u6cd5\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u5b9e\u4f8b\u5bf9\u8c61\u6765\u8c03\u7528\u3002\u9759\u6001\u65b9\u6cd5\u5e38\u7528\u4e8e\u4e0d\u4f9d\u8d56\u4e8e\u5b9e\u4f8b\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u5de5\u5177\u65b9\u6cd5\u6216\u8f85\u52a9\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u9759\u6001\u65b9\u6cd5\u901a\u8fc7 <code>static<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u5e76\u4e14\u53ea\u80fd\u901a\u8fc7\u7c7b\u6765\u8c03\u7528\uff0c\u800c\u65e0\u6cd5\u901a\u8fc7\u5b9e\u4f8b\u5bf9\u8c61\u6765\u8c03\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5b9a\u4e49\u9759\u6001\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>\u9759\u6001\u65b9\u6cd5\u7684\u5b9a\u4e49\u4f7f\u7528 <code>static<\/code> \u5173\u952e\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyClass {\n  static myStaticMethod() {\n    console.log(\"This is a static method.\");\n  }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>myStaticMethod<\/code> \u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u4f7f\u7528 <code>static<\/code> \u5173\u952e\u5b57\u58f0\u660e\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>\u9759\u6001\u65b9\u6cd5\u4e0d\u4f9d\u8d56\u4e8e\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u6765\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MyClass.myStaticMethod();  \/\/ \u8f93\u51fa: This is a static method.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u9759\u6001\u65b9\u6cd5\u7684\u7528\u9014<\/strong><\/h3>\n\n\n\n<p>\u9759\u6001\u65b9\u6cd5\u4e00\u822c\u7528\u4e8e\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f85\u52a9\u529f\u80fd<\/strong>\uff1a\u63d0\u4f9b\u4e0e\u7c7b\u7684\u5b9e\u4f8b\u65e0\u5173\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u5de5\u5177\u7c7b\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u5de5\u5382\u65b9\u6cd5<\/strong>\uff1a\u53ef\u4ee5\u7528\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u521d\u59cb\u5316\u7c7b\u7684\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u5171\u4eab\u72b6\u6001<\/strong>\uff1a\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u9759\u6001\u5c5e\u6027\uff0c\u800c\u9759\u6001\u5c5e\u6027\u662f\u7c7b\u672c\u8eab\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u662f\u5b9e\u4f8b\u7684\u5c5e\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u793a\u4f8b\uff1a\u9759\u6001\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 1\uff1a\u8f85\u52a9\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MathUtils {\n  static add(x, y) {\n    return x + y;\n  }\n\n  static subtract(x, y) {\n    return x - y;\n  }\n}\n\nconsole.log(MathUtils.add(5, 3));      \/\/ \u8f93\u51fa: 8\nconsole.log(MathUtils.subtract(5, 3)); \/\/ \u8f93\u51fa: 2<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MathUtils<\/code> \u7c7b\u4e2d\u7684 <code>add<\/code> \u548c <code>subtract<\/code> \u662f\u9759\u6001\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 2\uff1a\u5de5\u5382\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Circle {\n  constructor(radius) {\n    this.radius = radius;\n  }\n\n  getArea() {\n    return Math.PI * this.radius * this.radius;\n  }\n\n  static createCircle(radius) {\n    return new Circle(radius);\n  }\n}\n\nconst circle1 = Circle.createCircle(5);\nconsole.log(circle1.getArea());  \/\/ \u8f93\u51fa: 78.53981633974483<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>createCircle<\/code> \u662f\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa <code>Circle<\/code> \u7c7b\u7684\u5b9e\u4f8b\u3002\u901a\u8fc7 <code>Circle.createCircle(5)<\/code> \u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a <code>Circle<\/code> \u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 3\uff1a\u9759\u6001\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Counter {\n  static count = 0;\n\n  constructor() {\n    Counter.count++;\n  }\n\n  static getCount() {\n    return Counter.count;\n  }\n}\n\nnew Counter();\nnew Counter();\nconsole.log(Counter.getCount());  \/\/ \u8f93\u51fa: 2<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>count<\/code> \u662f\u9759\u6001\u5c5e\u6027\uff0c\u5b83\u5c5e\u4e8e\u7c7b\u672c\u8eab\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><code>getCount<\/code> \u662f\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u6765\u8fd4\u56de <code>count<\/code> \u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u4e0d\u80fd\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u8bbf\u95ee\u5b9e\u4f8b\u5c5e\u6027<\/strong><\/h3>\n\n\n\n<p>\u9759\u6001\u65b9\u6cd5\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u7c7b\u7684\u5b9e\u4f8b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8981\u8bbf\u95ee\u5b9e\u4f8b\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u5fc5\u987b\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n  constructor(name) {\n    this.name = name;\n  }\n\n  static greet() {\n    \/\/ \u4e0d\u80fd\u8bbf\u95ee\u5b9e\u4f8b\u7684\u5c5e\u6027 name\n    console.log(this.name); \/\/ undefined\n  }\n}\n\nPerson.greet();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <code>greet<\/code> \u9759\u6001\u65b9\u6cd5\u4e2d\uff0c\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5b9e\u4f8b\u5c5e\u6027 <code>name<\/code>\uff0c\u56e0\u4e3a\u9759\u6001\u65b9\u6cd5\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u9759\u6001\u65b9\u6cd5\u7ee7\u627f<\/strong><\/h3>\n\n\n\n<p>\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u5c31\u50cf\u5b9e\u4f8b\u65b9\u6cd5\u4e00\u6837\uff0c\u5b50\u7c7b\u53ef\u4ee5\u901a\u8fc7 <code>super<\/code> \u5173\u952e\u5b57\u8c03\u7528\u7236\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  static speak() {\n    console.log(\"Animal makes a sound\");\n  }\n}\n\nclass Dog extends Animal {\n  static speak() {\n    super.speak(); \/\/ \u8c03\u7528\u7236\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\n    console.log(\"Dog barks\");\n  }\n}\n\nDog.speak();\n\/\/ \u8f93\u51fa:\n\/\/ Animal makes a sound\n\/\/ Dog barks<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Dog<\/code> \u7ee7\u627f\u4e86 <code>Animal<\/code> \u7c7b\u7684\u9759\u6001\u65b9\u6cd5 <code>speak<\/code>\uff0c\u5e76\u4e14\u901a\u8fc7 <code>super.speak()<\/code> \u8c03\u7528\u4e86\u7236\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9759\u6001\u65b9\u6cd5\u662f\u4e0e\u7c7b\u672c\u8eab\u76f8\u5173\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u4e0e\u7c7b\u7684\u5b9e\u4f8b\u76f8\u5173\u3002<\/li>\n\n\n\n<li>\u9759\u6001\u65b9\u6cd5\u901a\u8fc7 <code>static<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u3002<\/li>\n\n\n\n<li>\u9759\u6001\u65b9\u6cd5\u53ea\u80fd\u901a\u8fc7\u7c7b\u6765\u8c03\u7528\uff0c\u800c\u65e0\u6cd5\u901a\u8fc7\u5b9e\u4f8b\u5bf9\u8c61\u6765\u8c03\u7528\u3002<\/li>\n\n\n\n<li>\u9759\u6001\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5de5\u5177\u51fd\u6570\u3001\u5de5\u5382\u65b9\u6cd5\u7b49\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0c\u9759\u6001\u65b9\u6cd5\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2100","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2100"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2100\/revisions"}],"predecessor-version":[{"id":2123,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2100\/revisions\/2123"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}