Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node.js – クラスにメンバ変数・メンバ関数を定義する ( インスタンス変数、インスタンス関数的な ) #3601

Open
YumaInaura opened this issue Apr 19, 2024 · 0 comments

Comments

@YumaInaura
Copy link
Owner

YumaInaura commented Apr 19, 2024

バージョン

  • node v20

コード例

オブジェクト ( インスタンス ) 作成時に設定した boolean が返る メンバ変数、メンバ関数をそれぞれ一つずつ設定する

class ExampleClass {
  constructor(flag) {
    this.flag = flag
  }

  getFlag() {
    return this.flag
  }
}

const exampleInstance1 = new ExampleClass(true)
console.log(exampleInstance1.flag) // true
console.log(exampleInstance1.getFlag()) // true

const exampleInstance2 = new ExampleClass(false)
console.log(exampleInstance2.flag) // false
console.log(exampleInstance2.getFlag()) // false

エラー例

ちなみに以下のような書き方はできないようだ

class ExampleClass {
  flag: boolean // SyntaxError: Unexpected identifier 'flag'
  constructor(flag: boolean) { // SyntaxError: Unexpected token ':'
    this.flag = flag
  }

  getFlag() {
    return this.flag
  }
}

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

プロフィール・経歴

https://github.com/YumaInaura/YumaInaura

@YumaInaura YumaInaura changed the title node.js – クラスとインスタンス変数、インスタンス関数 ( メンバ変数・メンバ関数 ) を定義する node.js – クラスにメンバ変数・メンバ関数を定義する ( インスタンス変数、インスタンス関数的な ) Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant