‘this’ in function inside prototype function [duplicate]

sampleObject.prototype.getFoo = function() {
 var me = this;
 var nested = function() {
  return me.foo;
 }
 return nested;
}

By saving the value of this in a local variable, you make it explicitly part of the lexical context for that function and for all nested function scopes. Thus, on the call to “nested”, that inner function will have its own scope (it’s own this value), but it can still refer to the variable “me” in the enclosing scope.

Leave a Comment