A
A
Alexander Shvedov2019-11-20 09:44:12
Vue.js
Alexander Shvedov, 2019-11-20 09:44:12

expression const self = this; what is the difference from this?

I stumbled upon such a code in the developer's code when I bring my properties to me through this, a null error falls and with self the question works why is it so to read the material or briefly someone can tell I know that in python this is an analogue of this PS please do not hate
const self = this;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2019-11-20
@KickeRockK

You did not provide a piece of code where it is used, so I would venture to suggest that this is to save the this context for some piece of code.
study

R
Robur, 2019-11-20
@Robur

Read what is this in JS and how it works.
Briefly - there, for sure, self is used inside functions that are called from somewhere outside and this will change for them, so a reference to the desired object is simply stored in self so as not to be lost.
In general, this was written about 5 years ago, now it is possible without it.
for vue there is vue-class-component which makes the code noticeably better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question