export default class Hello
{
_name: string = "abc";
get name()
{
console.log("原始 get name ");
return this._name;
}
set name(v:string)
{
console.log("原始 set name ", v);
this._name = v;
}
}
export class HelloExtend
{
static Init()
{
var name_source = Object.getOwnPropertyDescriptor(Hello.prototype, "name");
Object.defineProperty(Hello.prototype, "name",
{
get: function ()
{
console.log("扩展 get name");
return name_source.get();
},
set: function (v)
{
console.log("扩展 set name", v);
name_source.set(v + "---abcdefg");
},
enumerable: true,
configurable: true
});
}
}
测试代码
HelloExtend.Init();
var h = new Hello();
console.log(h.name);
h.name = 'aa';
console.log(h.name);
输入日志
bundle.js:523 扩展 get name
bundle.js:504 原始 get name
bundle.js:566 undefined
bundle.js:527 扩展 set name aa
bundle.js:508 原始 set name aa---abcdefg
bundle.js:523 扩展 get name
bundle.js:504 原始 get name
bundle.js:568 aa---abcdefg