我需要一个带有匿名setter和getter的Typescript(可能是JavaScript)对象。 我想要类似的东西:
class clsX{
a : string,
b : any,
public get () : any { return someFuncG(); }
public set (v: any) { someFuncS(v); }
}
var x : clsX;
var y : any;
y = x; // y assigned with return of someFuncG.
x = y; // execute someFuncS( y )
这可以在C#中轻松实现(使用强制转换运算符),但是我想知道在打字稿中是否有可能。一种装箱和拆箱。
答案 0 :(得分:0)
您想要的实际上是以下内容
:function dummyGet() {
console.log('We are getting something');
return 1;
}
function dummySet(value) {
console.log('We are setting something');
console.log('The new value is', value);
}
Object.defineProperty(
window, // `global` in Node.js
'x',
{
get() { return dummyGet(); },
set(x) { dummySet(x); }
}
);
但是似乎很难与类语法相关。