0

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?

2
  • You are confusing this context. Assuming that code shown will be inside an event handler this will have context of that callback function Commented Jul 2, 2016 at 23:31
  • 1
    Possible duplicate of Substitute Javascript this to Typescript -- this will answer your question. Commented Jul 3, 2016 at 2:36

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.