0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javascript 備忘録5<関数>

Last updated at Posted at 2021-04-12

■クラス定義

// ES6以降の書き方
class Person {
// コンストラクタ
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
// メソッドの定義
    hello() {
        console.log('こんにちは');
    }
}

// インスタンス化
// JSではオブジェクトとなることに注意
const me = new Person('Mike', 23);

■クラス継承
→上で定義したPersonクラスを継承して、newManクラスを作成する

class newMan extends Person {
    constructor(name, age, gender) {
        // superによりPersonのクラスを継承する
        // 親のメソッドを先に初期化しないとエラーになる
        super(name, age);
        this.gender = gender;
    }
    hello() {
        console.log('こんにちは');
    }
    // newManクラス特有のメソッドを定義する
    bye() {
        console.log('さようなら');
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?