Web 前端面向对象科普
来个缘分
https://www.zhihu.com/answer/2484559536
举个栗子
import { observable, reaction } from 'mobx';
type Gender = 'male' | 'female';
class Human {
@observable
accessor lover: Human | undefined;
constructor(public gender: Gender, public birthDay: Date) {
reaction(
() => this.lover,
lover => console.log(lover ? `💖${lover}` : '😭')
);
}
introduce() {
console.log(`I'm a ${this.gender} born at ${this.birthDay}`);
}
love(person: Human) {
this.lover = person;
}
}
const girl = new Human('female', new Date('2000-01-01')),
boy = new Human('male', new Date('2000-01-01'));
girl.introduce();
boy.introduce();
boy.love(girl);
girl.love(boy);