JavaScript this Object: Tutorials and Examples

This JavaScript article tutorial guides you a full completed instructions and JavaScript code examples to use this JavaScript object. You can learn many basic JavaScript knowledges such as scope, call a function/method, etc.

Please go to the full post page for all details or read other related JavaScript tutorials:
- JavaScript Objects: Compare and Clone
- Let Get Started Object-Oriented Programming JavaScript
- Object JavaScript Test Quiz
- Simple Concepts about Types and Objects in JavaScript OOP
- Object Oriented JavaScript Techniques


Sampled by © JavaScriptBank.com

JavaScript is a great programming language. That would have been a controversial statement a few years ago, but developers have rediscovered its beauty and elegance. If you dislike JavaScript, it’s probably because:

  • You’ve encountered browser API differences or problems — which isn’t really JavaScript’s fault.
  • You’re comparing it to a class-based language such as C++, C# or Java — and JavaScript doesn’t behave in the way you expect.

One of the most confusing concepts is the ‘this’ keyword. In most languages, ‘this’ is a reference to the current object instantiated by the class. In JavaScript, ‘this’ normally refers to the object which ‘owns’ the method, but it depends on how a function is called.

Global Scope

If there’s no current object, ‘this’ refers to the global object. In a web browser, that’s ‘window’ — the top-level object which represents the document, location, history and a few other useful properties and methods.



window.WhoAmI = "I'm the window object";
alert(window.WhoAmI);
alert(this.WhoAmI); // I'm the window object
alert(window === this); // true

Calling a Function

‘this’ remains the global object if you’re calling a function:


window.WhoAmI = "I'm the window object";
function TestThis() {
	alert(this.WhoAmI); // I'm the window object
	alert(window === this); // true
}
TestThis();

Calling Object Methods

When calling an object constructor or any of its methods, ‘this’ refers to the instance of the object — much like any class-based language:


window.WhoAmI = "I'm the window object";
function Test() {
	this.WhoAmI = "I'm the Test object";
	this.Check1 = function() {
		alert(this.WhoAmI); // I'm the Test object
	};
}
Test.prototype.Check2 = function() {
	alert(this.WhoAmI); // I'm the Test object
};
var t = new Test();
t.Check1();
t.Check2();

Using Call or Apply

In essence, call and apply run JavaScript functions as if they were methods of another object. A simple example demonstrates it further:


function SetType(type) {
	this.WhoAmI = "I'm the "+type+" object";
}
var newObject = {};
SetType.call(newObject, "newObject");
alert(newObject.WhoAmI); // I'm the newObject object
var new2 = {};
SetType.apply(new2, ["new2"]);
alert(new2.WhoAmI); // I'm the new2 object

The only difference is that ‘call’ expects a discrete number of parameters while ‘apply’ can be passed an array of parameters.

That’s ‘this’ in a nutshell! However, there are several gotchas which may catch you out. We’ll discuss those in my next chapter

In above chapter, we looked at JavaScript�s this statement and how it can change depending on the context of the function call. Today, we�ll examine several situations where this could catch you out�

1. Forgetting �new�

Consider the following code:



window.WhoAmI = "I'm the window object";
function Test() {
  this.WhoAmI = "I'm the Test object";
}
var t = Test();
alert(window.WhoAmI); // I'm the Test object
alert(t.WhoAmI); // t is undefined

What we really meant is:


var t = new Test();

The omission of the new statement gave us undesirable results. Other languages would throw an error when faced with a direct call to a constructor but JavaScript simply treats it like any other function call. this is taken to be the global window object and no value is returned from Test() so t becomes undefined.

This situation can be fixed if you�re writing a JavaScript library for third-party developers. Refer to Fixing Object Instances in JavaScript.

2. Module madness

This one will give you a headache. Examine the following code which uses a module pattern:



window.WhoAmI = "I'm the window object";
var Module = function() {
  this.WhoAmI = "I'm the Module object";
  function Test() {
    this.WhoAmI = "I'm still the Module object";
  }
  return {
    WhoAmI: WhoAmI,
    Test: Test
  };
}();
alert(Module.WhoAmI); // I'm the Module object
alert(window.WhoAmI); // I'm the Module object
Module.Test();
alert(Module.WhoAmI); // I'm still the Module object

The code looks logical � so why is window.WhoAmI saying it�s the module object?

We need to remember that we have a self-executing function. It�s results are returned to the Module variable but, when it�s first run, Module doesn�t exist. this is therefore the global window object. In other words, this.WhoAmI = window.WhoAmI = "I'm the Module object".

The function returns a JavaScript object with a WhoAmI property with a value of 'WhoAmI'. But what does that refer to? In this case, the JavaScript interpreter works up its prototype chain until it magically finds window.WhoAmI ("I'm the Module object").

Finally, we run the Test() method. However, Module has now been created so, within the Test function, this refers to the Module object so it can correctly set the WhoAmI property.

In summary, avoid using this within a module to refer to the module itself! You should never need it.

3. Method misconceptions

Here�s another JavaScript pattern which will screw with your synapses:



var myObject = {};
myObject.method = function() {
  this.WhoAmI = "I'm myObject.method";
  function Test() {
    this.WhoAmI = "I'm myObject.method.Test()";
  }
  Test();
  return this.WhoAmI;
};
alert(myObject.method()); // I'm myObject.method

In this example, Test() is a private function executed within myObject.method(). At first glance, you would expect this within Test() to reference myObject. It doesn�t: it refers to the global window object since it�s just another function.

If you want to reference myObject within the private function, you�ll require a closure, for example:



var myObject = {};
myObject.method = function() {
  this.WhoAmI = "I'm myObject.method";
  var T = this;
  function Test() {
    T.WhoAmI = "I'm myObject.method.Test()";
  }
  Test();
  return this.WhoAmI;
};
alert(myObject.method()); // I'm myObject.method.Test()

4. Referencing methods

Here�s a little code which, fortunately, will work exactly as you expect:



var myObject = {};
myObject.WhoAmI = "I'm myObject";
myObject.method = function() {
  this.WhoAmI = "I'm myObject.method";
};
// examine properties
alert(myObject.WhoAmI); // I'm myObject
myObject.method();
alert(myObject.WhoAmI); // I'm myObject.method

Let�s make a minor change and assign myObject.method to another variable:



// examine properties
alert(myObject.WhoAmI); // I'm myObject
var test = myObject.method;
test();
alert(myObject.WhoAmI); // I'm myObject

Why hasn�t myObject.WhoAmI changed? In this case, the call to test() acts like a regular function call so this refers to the window object rather than myObject.

If you think that�s nasty, wait until we take a look at JavaScript event handlers in my next post!

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
Insights for Advanced Zooming and Panning in JavaScript Charts
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website


Top view articles
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
Top 10 Beautiful Christmas Countdown Timers
Top 10 Best JavaScript eBooks that Beginners should Learn
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com