Greg Gum
Greg Gum

Reputation: 37909

Converting "this" from jquery to typescript

I have this bit of code which works.

I now want to use it in a typescript app. I still want to use jquery, but I don't understand how to convert the use of this to what Typescript needs to work.

if ($(this).width() < 769) {
    $('body').addClass('page-small');
} else {
    $('body').removeClass('page-small');
    $('body').removeClass('show-sidebar');
}

In typescript, this refers to the current class. What do I replace that with so it works as expected?

Upvotes: 0

Views: 743

Answers (1)

basarat
basarat

Reputation: 275947

I still want to use jquery, but I don't understand how to convert the use of this to what Typescript needs to work.

Move the function out of the class and call it from the class. Alternatively create a self variable to use both this(class) and this(jquery).

More

https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html#tip-arrow-functions-with-libraries-that-use-this

Upvotes: 1

Related Questions