THIS, Function Context, And Object Literals In Javascript

Posted October 13, 2009 at 8:42 AM

Tags: Javascript / DHTML

In Javascript, the "this" keyword points to the context of the currently executing function. Previously, I had thought that "this" would only work at the object level (treat the object as the context) if the given object was created using the "new" operator. Last night, while finishing up jQuery In Action, I found out that this is not true. A function's context will correctly bind to the parent object even if that parent object is defined as an object literal.

To see this in action, take a look at the following example:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>Function Context And Object Literals</title>
  • <script type="text/javascript">
  •  
  • // Define an object literal with properties as well as
  • // a few accessor (getter) methods. Notice that the
  • // accessor use the THIS reference, which will properly
  • // define the context as the parent object, "girl".
  •  
  • var girl = {
  • name: "Phoebe",
  • hair: "Brunette",
  •  
  • getName: function(){
  • return( this.name );
  • },
  • getHair: function(){
  • return( this.hair );
  • }
  • };
  •  
  •  
  • // Call one of the object methods to see what the given
  • // function context is (what the THIS will point to).
  •  
  • console.log( girl.getHair() );
  •  
  • </script>
  • </head>
  • <body>
  • <!--- Nothing here. --->
  • </body>
  • </html>

As you can see, we are creating the object - girl - as an object literal using JSON (Javascript Object Notation). The object contains two properties and two accessor methods. The accessor methods both make reference to the "this" keywords which points to the function context. Because the context properly binds to the parent object - girl - calling the accessor method in the code above returns the corresponding property:

Brunette

That's good to know. I think I still prefer creating objects using Function definitions and the "new" operator as it provides a mechanism for multiple instantiation; but, when I need a really lightweight object definition, this seems like a nice approach.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Oct 13, 2009 at 9:56 AM // reply »
21 Comments

I absolutely does, and is quite useful. Where things get hairy is that jQuery callbacks alter the THIS scope, and it varies based on what the function executing the callback is (eg. $.ajax success callback, THIS = the ajax parameter object).

This is pretty easily solved by keeping a copy of THIS in a variable (eg. var __this = this;), and from there closures can bubble back up the parent to get to the original THIS via our copy.

Good stuff, and keep up the blogging Ben.


Oct 13, 2009 at 11:13 AM // reply »
7,560 Comments

@Adam,

Yeah, the jQuery stuff is cool though. I think you just start to get used to it. Once you accept the fact that "this" rarely points to the jQuery collection (outside of plugin development), you kind of just assume the next most appropriate thing - the raw element in question.


Oct 13, 2009 at 4:21 PM // reply »
1 Comments

Right, 'this' is determined at the time a function is called. It has no relation to how the object is created or what type of object it is.

When you call any function using foo.bar() notation, 'this' is the foo object.

It also works the same way when you call the function with foo['bar']() or foo[variableContainingMethodName]() notation.

You lose the connection to the original object if you take a reference to the function:

var moo = foo.bar;
moo(); // 'this' is the window object!

As you know, you can also explicitly set 'this' at the time you call a function:

bar.apply( someObject ); or
bar.call( someObject, arg, arg );

Inside the 'bar' function, 'this' will be the someObject you passed in.


Oct 15, 2009 at 8:32 AM // reply »
7,560 Comments

@Michael,

I rather like this. ColdFusion works in the same way - functions are given context only when they are called as a member of a given object.

For some reason, I just had it in my mind that if an object wasn't created using new/() then it would be bound to the window.

This is way cool though.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 19, 2010 at 9:29 AM
Be Careful When Including Images In jQuery Auto-Suggest
@Jim, I've only ever messed with the config in FireFox. Not sure what's available in the other browsers. ... read »
Mar 19, 2010 at 9:27 AM
ColdFusion 8 ImageDrawTextArea() (Inspired By Barney Boisvert!)
@Martin, It's been a while since I looked at this; but, I don't think I handled explicit line breaks because it just made the calculations harder. Perhaps in another version. ... read »
Mar 19, 2010 at 9:23 AM
Ask Ben: Parsing CSV Strings With Javascript Exec() Regular Expression Command
@Jens, Ahh, gotcha. Yeah, the textarea is probably gonna be safe cross-browser. ... read »
Mar 19, 2010 at 9:22 AM
How ColdFusion CreateObject() Really Works With Java Objects
@Dmitry, Word up - createObject() can do a whole bunch of things. It's probably one of the best updates to the language. ... read »
Mar 19, 2010 at 9:19 AM
Learning ColdFusion 9: Resetting Applications With ApplicationStop()
@Rocky, Yeah, I assume it will flush all application-scope variables when you can applicationStop(), regardless of where they are initialized. As far as the connectivity between application and s ... read »
Mar 19, 2010 at 9:15 AM
Ask Ben: Getting The Start And End Dates Of The Previous Week In ColdFusion
@David, It gets the last week. I goes back 7 days, and then finds the start / end of that week. ... read »
Mar 19, 2010 at 9:12 AM
Using Inline List Elements
@ben: thank you very much - that helped me a lot ;) ... read »
Mar 19, 2010 at 9:10 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
@Tom, That's odd. Looks like a malformed XML problem. Make sure you aren't missing any of the ">" closing brackets or something. ... read »