82603

Weird JavaScript statement, what does it mean?

I see this code, what's it doing?

var obj = this;

Answer1:

It's just storing the current reference of this object, to be used in future. It's useful, because in JS value of this depends on a context.

Answer2:

It saves a reference to whatever this was in the current context, so it can be used later.

Answer3:

That is setting a local copy of the current first class function that its being set in.

This is used ALOT in jquery as this takes on a different meaning when you being using the selectors.

Say I have a

function Person() { this.name = "gnostus"; }

and I need to access name from inside a jquery selector, where this becomes an html element, I would store my object into a copy variable and use, obj.name in place of this.name when im inside of the jquery context.

Answer4:

It depends where this statement is located. It assigns to variable "obj" reference to current object.

for example the following code will open an alert window and show [Window object]. That's because we check value of "this" in the body area (not inside any objects event handler, etc.)

<html> <head> </head> <body> <script type="text/javascript"> alert(this); </script> </body> </html>

Answer5:

It's creating a variable 'obj' and setting it to the current context.

So, for example, if it's at a global level this would be the current DOM Window.

Answer6:

var obj = this;

Is stating, assign obj with the parent of the current scope.

I first read this post a couple months ago to get a handle on the keyword 'this'.

http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/

Answer7:

The this keyword is used to refer to the owner of the function , or the variable the this keyword is used in. For a detailed understanding visit http://www.quirksmode.org/js/this.html

Answer8:

The only context I can think of where this sort of code makes sense is to make the current context (this) available inside a closure.

So the code would be something like:

var obj = this; setTimeout(function() { obj.someMethod(); }, 1000);

That would call the method "someMethod" on the current context object after 1 second goes by.

Recommend

  • How to access recipient on sent messages page with mailboxer
  • R sqldf renaming a field in a select statement
  • .NET video play library which allows to change the playback rate?
  • How to 'create temp table as select' in Slick?
  • Python pickle not one-to-one: different pickles give same object
  • Looking for good analogy/examples for monitor verses semaphore
  • converting text file into xml using php?
  • MySQL Order by column = x, column asc?
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Excel's Macro-Recorder usage
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • How to use carriage return with multiple line?
  • Android full screen on only one activity?
  • MongoDB in PHP using aggregate to group by _id is null not working
  • How do I change content of ComboFieldEditor?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Problems to linebreak with an int in JLabel
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Resize panoramic image to fixed size
  • How to add date and time under each post in guestbook in google app engine
  • Fill an image in a square container while keeping aspect ratio
  • htaccess rewriting URLs with multiple forward slashes
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Symfony2: How to get request parameter
  • How do you troubleshoot character encoding problems?
  • jquery mobile loadPage not working
  • Do I've to free mysql result after storing it?
  • How to format a variable of double type
  • How to get icons for entities from eclipse?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • need help with bizarre java.net.HttpURLConnection behavior
  • JaxB to read class hierarchy
  • Django query for large number of relationships
  • How to Embed XSL into XML