I am working with prototype methods and here is the scenerio
function Foo () {
this.x = 5;
this.y = 2;
this.z = this.addValues();
}
Foo.prototype = {
addValues: function (){
return this.x + this.y;
}
}
Obviously this is just a simple example; in real project, there will be lot of activities in the ‘addValue’ function. Is it fine to use ‘this’ keyword 100s of times or caching this to local variable helps any performance improvements. For example, the below will make any difference?
Foo.prototype = {
addValues: function (){
var self = this;
return self.x + self.y;
}
}
thisis the standard way to accessxandy. You’ll get no improvements from cachingthisto a local var—if anything, you’re wasting space by declaringselfin the first place.The only possible risk you’d see is with something like this:
Having said that, I don’t think you’re responsible for people misusing your function, and I wouldn’t worry about it.
Also, by convention, functions meant to be used as constructors should start with a capital letter. Consider renaming
footoFoo