Search code examples
jquerytypescripttypescript1.8

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?


Solution

  • 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