クラス構文
夜に活動があったり(Ikejiri.rbオンライン読書会)、夜中にムスメ対応が発生すると、睡眠時間がずれてしまう。。 仕方なし、そういうときは受け入れて、またできるときに、やる。
クラス構文
constructor は継承されないので、継承先で constructor() と明示的に書く必要がある。
class Parent {
constructor(name) {
this.name = name;
}
greet() {
console.log(`わたしは${this.name}です`);
}
static explain(name) {
console.log(`${name}には子どもがいます`);
}
}
class Child extends Parent {
constructor(name) {
super(name);
}
cry() {
console.log(`${this.name}が泣きました`);
}
}
const person1 = new Parent('かとりえ');
person1.greet(); // わたしはかとりえです
Parent.explain('もじゃ'); // もじゃには子どもがいます
const person2 = new Child('ムスメ');
person2.cry(); // ムスメが泣きました
なるほど?
いまいちまだ this がよくわからない。。