我正在编写一个简单的Process类,并提供了以下代码(这只是摘录,不是完整的类定义):
class Process {
private waitingContext?: {
promise: Promise<number>,
resolve: (exitCode: number) => void,
reject: (err: any) => void
};
wait(): Promise<number> {
if (this.waitingContext)
return this.waitingContext.promise;
const promise = new Promise<number>(
(resolve: (exitCode: number) => void, reject: (err: any) => void) => {
this.waitingContext = {promise, resolve, reject};
});
return promise;
}
}
令我困惑的是,在构造对象和进行赋值之前,如何引用新创建的变量promise
。用JavaScript正常吗?