谁使用过typescript Decorators特性?

本来想利用装试者来监听数据更改. 代码如下:

export function fireBind(type:string) {        return function (taget:any, key:string, descriptor:TypedPropertyDescriptor<any>)            {            let oldSet = descriptor.set;
            descriptor.set = function(val){
                MessageCenter.fire(new ModelEvent(type, val));                console.log('1111');
                oldSet.call(null, val);                console.log('2222');
            }
        }
    }
class Test {    private _selected;    @fireBind(EventState.GOODS_SELECTED)    public set selected(data:any) {        this._selectedData = data;        console.log('this._selectedData:', this._selectedData);
    }    public get selected(){         return this._selectedData;
  }
}

调用 :

const T = new Test();
T.selected = 'test';console.log(T.selected);

输出结果:
1111
this._selectedData: test
null;


海绵宝宝撒
浏览 714回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript