Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@setta

親クラスから派生クラスのメソッドを呼ぶ

More than 1 year has passed since last update.

前書

抽象クラス的なことがしたかったので

呼び方

非staticメソッドからstaticメソッドを呼ぶ場合のみ呼び方が異なる
「非staticメソッドから非staticメソッド」と「staticメソッドからstaticメソッド」の呼び方は同じ
何が起きているかはなんとなく分かるけど、もっとわかりやすくならなかったものか

"use strict";
class Parent {
    static yourMethod(msg){
        this.hisMethod(msg);
    }

    myMethod(msg){
        this.constructor.hisMethod(msg);
        this.herMethod(msg);
    }
}

class Child extends Parent {
    static hisMethod(msg) {
        console.log('Child static', msg);
    }

    herMethod(msg){
        console.log('Child instance', msg);
    }
}

Child.yourMethod(1);//Child static 1

const child = new Child(); 

child.myMethod(2);//Child static 2, Child instance 2

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
setta

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?