2019-09-03 JavaScript (OOP) ES6 getter & setter

const _radius = new WeakMap();

class Circle {
    constructor(radius) {
        _radius.set(this, radius);
    }

    get radius() {
        return _radius.get(this);
    }

    set radius(value) {
        if (value <= 0) throw new Error('invalid radius');
        _radius.set(this, value);
    }
}

const c = new Circle(1);

2019-09-03 JavaScript (OOP) ES6 getter & setter_第1张图片

你可能感兴趣的:(前端,JavaScript)