Reputation: 35843
Given the following TypeScript code snippet:
export class MyClass {
myMethod() {
// ...
$myQuery.each(function(idx, elm) {
$(this)... // Original javascript code which obviously not correct in typescript
}
}
}
However in TypeScript this in a class method "this" always refers to the class instance. I would like to access the very same object what it would be in pure javascript.
In general: What is the way to access the original javascript context (this) in a callback when using TypeScript?
Upvotes: 2
Views: 1546
Reputation: 4185
It is not accurate.
When using this
in lambda expression or in a class method, it refers to the class itself. Examples:
class A{
public a:number;
public foo(){
this.a = 1;//this here is A
var lambda = () => { this.a = 2; } //this here is A
var fn = function() { this.a = 3; } // this here is not the A
}
}
You can look at the transpiled code here: Typescript Playground
Upvotes: 2