Quantcast [programming] Understanding The "this" Keyword In Javascript
Search by tag or site Login to my blog ? Start my own blog














TheMoneyBlogs
Home
About
Create your own blog
Contact us
Vote for this blog!

Jon Aquino's Mental Garden

Technologies 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:
    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.
Stock Quote or
Examples
Morpheus Trading - Mon Jul 21, 2008 08:33AM
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]
Millionaire Now! by Larry Nusbaum - Tue Jul 22, 2008 09:23AM
Hedge funds have made billions this year shorting the banks, [read more]

PREMIER SPONSORED LINKS

Most Visited Blogs | Most Popular Blogs | Most Recent Blogs | Contact Us | Terms and conditions | Privacy Policy

The columns, articles, message board posts and any other features provided on TheMoneyBlogs.com are provided for personal finance, education and investment information and are not to be construed as investment advice. Under no circumstances does the information in this content represent a recommendation to buy, sell or hold any security. The views and opinions expressed in an article or column are the author's own and not necessarily those of TheMoneyBlogs.com and there is no implied endorsement by TheMoneyBlogs.com of any advice or trading strategy. The analysts and employees or affiliates of TheMoneyBlogs.com may hold positions in the stocks or industries discussed here. Your use of this and all information contained on TheMoneyBlogs.com is governed by the Terms and Conditions of Use. Please click the link to view those terms. Follow this link to read our Editorial Policy.

Copyright © 2008 The Connors Group, Inc.