Help us understand the problem. What is going on with this article?

javascript class構文でstaticプロパティ

More than 1 year has passed since last update.

javascript の class構文 確かに便利だが…

class Foo {
    static message : "hello foo!"
}

と書けないですね。

let Foo = Object.assign(class {
    get message () {
        return this.constructor.message === undefined ?
            super.message : this.constructor.message;
    }

    set message (v) {
        this.constructor.message = v;
        return true;
    }
},{message: "hello foo!"})

let Bar = class extends Foo {}

//テスト 出力は hello foo!
console.log(Bar.message);

Bar.message = "hello bar!";// Object.assign(Bar, {message: "hello bar!"})と挙動は同じ
console.log(Foo.message);
console.log(new Foo().message);
console.log(Bar.message);
console.log(new Bar().message);

new Bar().message = undefined;
console.log(Foo.message);
console.log(new Foo().message);
console.log(Bar.message);
console.log(new Bar().message);

// staticかどうかチェック
new Foo().message = "bye foo!";
console.log(Foo.message);
console.log(new Foo().message);

Foo.message = undefined;
console.log(Foo.message);
console.log(Bar.message);

と書くことで、staticなプロパティになります。 参考までにどうぞ。

johnny-shaman
稚拙ですが、関数型プログラミングをするためのライブラリを公開しています。 https://www.npmjs.com/package/cte https://www.npmjs.com/package/dsand 日曜プログラミングのお供にでもいかがですか?(笑) プログラミングカフェ 札幌 主催
https://join.slack.com/t/p-cafe-sapporo/shared_invite/enQtNDc1MzEyNDcxNjIyLWI1ZDY1NWRhZjIxMzlkN2I1Mzg4ODBhODg2NjNlOGYwNDk2M2FjZWE2YTFmYjFjNTBiNGY5YTBjYWM2ZDc5NzM
qiitadon
Qiitadon(β)から生まれた Qiita ユーザー・コミュニティです。
https://qiitadon.com/
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした