javascript - observer 范例

    技术2022-05-20  40

    function observable (value, condition, callback){    this.value = value;    this.condition = condition;    this.callback = callback;}observable.prototype = {    get value () {        return this._value;    },    set value (value) {        this._value = value;        if (this.condition && this.callback && this.condition (value)) {            this.callback (value);        }    }};condition = function (value) {    console.log ('condition', value);    return value === 2;}callback = function (value) {    console.info ('Big Brother is watching you!');}var a = new observable (0, condition, callback);console.log ('set value to 1');a.value = 1;console.log ('set value to 2');a.value = 2;console.log ('set value to 3');a.value = 3;

    最新回复(0)