Main concern is efficiency.
I am working on javascript scopes and one thing that I am confused about is this
inside a function.
I have read many answers and I understand them. But what I am concerned about is efficiency. Have a look at my code.
class Fancy {
constructor () {
}
checkScope (callback) {
console.log('Inside checkScope');
callback ();
}
}
class Prog {
constructor () {
this.name = 'myProg';
this.fancy = new Fancy ();
}
run () {
var that = this;
this.fancy.checkScope(function () {
console.log('Name ', that.name);
});
}
}
var prog = new Prog ();
prog.run();
Now in run()
I am storing reference of this
in a local variable that
. This is working for me. But is it safe? Is it efficient? If no, please suggest me a good strategy/trick.
Thanks :)