「ES6」クラスのスタティックメソッド(static)を定義する

2021年10月13日

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class クラス名{
static メソッド名{処理コード}
}
使い方
クラス名.static関数名
class クラス名{ static メソッド名{処理コード} } 使い方 クラス名.static関数名
class クラス名{
  static メソッド名{処理コード}
}
使い方
クラス名.static関数名

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script>
//クラスの定義
class User {
//static メソッドfunAの定義
static funA() {
return "study skill in arkgame"
}
// sttaicメソッドfunBの定義
static funB() {
alert("chang fa tun")
}
}
//クラス名.スタティック関数名 funAを呼び出す
alert(User.funA());
// funBを呼び出す
User.funB();
</script>
<script> //クラスの定義 class User { //static メソッドfunAの定義 static funA() { return "study skill in arkgame" } // sttaicメソッドfunBの定義 static funB() { alert("chang fa tun") } } //クラス名.スタティック関数名 funAを呼び出す alert(User.funA()); // funBを呼び出す User.funB(); </script>
<script>
//クラスの定義
class User {
   //static メソッドfunAの定義
  static funA() {
    return "study skill in arkgame"
  }
  // sttaicメソッドfunBの定義
  static funB() {
     alert("chang fa tun")
  }
}
//クラス名.スタティック関数名 funAを呼び出す
alert(User.funA());
// funBを呼び出す
User.funB();
</script>

実行結果
study skill in arkgame
chang fa tun

ECMAScript/ES6

Posted by arkgame