Answer the question
In order to leave comments, you need to log in
How to set a variable for all child functions of a JS object?
I am developing a web application in which I actively use object prototypes.
For convenience, I would like to have a short name for accessing the object. This name could be this , but considering that in prototype functions I may have other functions where the scope will change - this option is not suitable.
Setting a variable and assigning this to it is also not an option, these are extra lines of code.
var myClass = function () {}
myClass.prototype {
foo: function () {
//тут this это myClass
var myClass = this;
function bar () {
//тут this это НЕ myClass, к нему можно обратиться только через переменную myClass
}
}
}
Answer the question
In order to leave comments, you need to log in
myClass.prototype {
foo: function () {
var bar = function () {
// теперь this работает
}.bind(this);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question