| Search by tag or site | Login to my blog ? Start my own blog |
Jon Aquino's Mental GardenTechnologies that make life wonderful |
[programming] Understanding The "this" Keyword In Javascript
Posted on 06/07/2008 19:52:31 | Link | Post Comment
My colleague asked me how to know what the "this" keyword refers to in JavaScript. It's a bit different from other languages.
First, the rule: When you invoke a function on an object, "this" refers to the object. When you invoke a function by itself, "this" refers to the global object (the window object).
Now for some examples.
Creating an object in JavaScript is easy:
Let's add a function to this object:
When we call food.getCalories(), "this" is the food object, because we are invoking the function on the food object.
But check this out:
Here we invoke the function by itself, i.e., not on an object. "this" is the global object (i.e. window), because we are invoking the function by itself.
Now look at this:
Here we've created an automobile object, and added our function to it. "this" is the automobile object, because we are invoking the function on the automobile object.
Thus, "this" is a dynamic quantity – it is not cast in stone when the function is defined, but refers to the current object that the function is being invoked on.
First, the rule: When you invoke a function on an object, "this" refers to the object. When you invoke a function by itself, "this" refers to the global object (the window object).
Now for some examples.
Creating an object in JavaScript is easy:
var food = {};Let's add a function to this object:
var food = {
getCalories: function() { alert(this); return 300; }
};
food.getCalories();When we call food.getCalories(), "this" is the food object, because we are invoking the function on the food object.
But check this out:
var getCals = food.getCalories;
getCals();
Here we invoke the function by itself, i.e., not on an object. "this" is the global object (i.e. window), because we are invoking the function by itself.
Now look at this:
var automobile = {};
automobile.getCalories = getCals;
automobile.getCalories();Here we've created an automobile object, and added our function to it. "this" is the automobile object, because we are invoking the function on the automobile object.
Thus, "this" is a dynamic quantity – it is not cast in stone when the function is defined, but refers to the current object that the function is being invoked on.
- Cool Service: Uptime Website Monitoring Service
- Svn Time-lapse View
- Royal Canadian Air Force "5bx" Daily 15-minute Exercise Program
- Simplicity And Difficulty Are Orthogonal
- October Challenge: Reading Knuth's "the Art Of Computer Programming"
- Oct 2007
- Sep 2007
- Aug 2007
- Jul 2007
- Jun 2007
- May 2007
- Apr 2007
- Mar 2007
- Feb 2007
- Jan 2007
- Dec 2006
- Nov 2006
- Oct 2006
- Sep 2006
- Aug 2006
- Jul 2006
- Jun 2006
- May 2006
- Apr 2006
![]()
Examples
Morpheus Trading - Mon Jul 21, 2008 08:33AM
NOTE: Please click on the charts below to enlarge them if [read more]
NOTE: Please click on the charts below to enlarge them if [read more]
Morpheus Trading - Mon Jul 21, 2008 08:31AM
NOTE: Please click on the charts below to enlarge them i [read more]
NOTE: Please click on the charts below to enlarge them i [read more]
Millionaire Now! by Larry Nusbaum - Tue Jul 22, 2008 09:23AM
Hedge funds have made billions this year shorting the banks, [read more]
Hedge funds have made billions this year shorting the banks, [read more]












<< My Home | TheMoneyBlogs Home