The object to which the current method was called is accessible through the special this variable. Each time you call a method on an object, this will refer to the object in the method.
var myExtension = { init: function() { this.onPageLoad(); }, onPageLoad: function() {
this always refers to the calling object, not to the object that is defined by the function or is a property.
value = 'global'; var ob0 = { value: 'foo', val: function() { return this.value; }, }, ob1 = {value: 'bar'}, ob2 = {value: 'baz'}; ob0.val();
In the latter case, val is executed as a free function (a function that is not bound to an object, that is, not a method), in which case this takes the value of a global object (which is a window in web browsers) as part of the val execution. Global variables are actually properties of a global object, so val() returns 'global' (the value of a global variable called value ). Since global variables are actually properties of a global object, you can view free functions as if they were actually methods of a global object. From this point of view, the last two lines (when executed in the global area) are equivalent:
window.val = ob0.val; window.val();
This point of view does not quite correspond to the reality of coverage, although you will notice a difference within the functions. In the function window.val = ... will create a global value, but var val will not.
value = 'global'; var ob0 = { value: 'foo', val: function() { return this.value; }, }; function lcl() { var val = ob0.val;
See the MDN man page for the call method described above. For more information on this variable, see " JavaScript" this "keyword " and " How does the keyword" this "work in a JavaScript literal object? "