[英]typescript anonymous getter and setter (kind of boxing and unboxing)
我需要一個帶有匿名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#中輕松實現(使用強制轉換運算符),但我想知道在打字稿中是否可行。 一種裝箱和拆箱。
您想要的實際上是以下內容:
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); }
}
);
但是似乎很難與類語法相關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.