class Profile {
private resource: number = 1;
/**
* Problem here
*/
async initialize(): Promise<void> {
console.log(this.resource);
}
}
let p = new Profile();
p.initialize();
let p = new Profile();
p.initialize();
I created this sample script which transpiles to
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
return new Promise(function (resolve, reject) {
generator = generator.call(thisArg, _arguments);
function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
function step(verb, value) {
var result = generator[verb](value);
result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
}
step("next", void 0);
});
};
class Profile {
constructor() {
this.resource = 1;
}
/**
* Problem here
*/
initialize() {
return __awaiter(this, void 0, Promise, function* () {
console.log(this.resource);
});
}
}
let p = new Profile();
p.initialize();
//# sourceMappingURL=main.js.map
and the output is
1
as expected. So the conclusion is that it's not about this keyword. It's about Resource class, I guess.
thisrefers to depends on how the method is called.this. But I just realized that you said it would refer to aProfileinstance. In that casethisdoesn't have aresourceproperty for whatever reason.