How can I avoid doing const self = this
in JavaScript/Node.js in each type function?
function Server(config) {
const self = this;
this.config = config;
setTimeout(function() {
console.log(self.config);
}, 2000);
}
Server.prototype.foo = function() {
const self = this;
setTimeout(function() {
console.log(self.config);
}, 4000);
};
module.exports = Server;
It is very error prone (do I use this
or self
) because you have to look at your scope. Not to mention it feels unnecessary to declare extra variables.