article

 

this

[WHAT]

  1. ] this - is a keyword in the javascript (and other) programming languages
  2. ] this - is used as a shortcut reference to refer to the current object,
  3. ] this - 'refers' to the object that invokes the call to this ...., ( aka the currently active object )
    1. ] First, know that all functions in JavaScript have properties, just as objects have properties.
    2. ] And when a function executes, it gets the this property
    3. —a variable with the value of the object that invokes the function where this is used.(SRC = 1)
  4. ] this reference ALWAYS refers to (and holds the value of) an object—a singular object—and
  5. ] this is usually used inside a function or a method,
  6. ] this can be used outside of a function in the global scope.
  7. ] this has some 'gotchas' which can make it a 'bit tricky/trickier/? to work with' in some cases
    1. ] this - inside a closure,
    2. ] this - global scope
    3. ] this - strict mode - Note that when we use strict mode, this holds the value of undefined in global functions and in anonymous functions that are not bound to any object.
  8. ] RELATE = ] scope, ] call, apply, bind, $.proxy()

[WHY]

  1. ]
  2. ] this is used to refer to || reference the 'currently active' object
  3. ] shorthand,
    1. [] see example 1
  4. ] the POWER of
    1. [] this lies WHERE you dont know the reference to something, ie a web page has 20 links, i dont know which link the user will click, BUT i can write 1 'link click' function and THEN using the 'this' function i could get the value of the link that you the user clicked, var youClicked = this.id; and avoid the pain of having to write an individual link click function for each link on the page
  5.  

[WHERE]

  1. ] * this is assigned a value when an object invokes the function where this is defined

[WHEN]

  1. ]
  2. ]

[EXAMPLE]

  1. ] to get a reference to the current pages title, i could use 
    1. ] var t = document.title;   OR because the window object has the current
    2. ] var t = this.title;  

[HOW-TO]

  1. ] the important part is knowing what .this is referring to at the time you are referencing it

[REFERENCE]

  1. [] understand javascripts this with clarity and master it
    1. [] by Richard Bovine @javascriptissexy.com -  an in depth concise and clear explanatory article on using / understanding this
  2. [] NEW  Object methods, " this"
    1. []  by __ @ javascript.info -
  3. []

 

 

 

 

 

 

Details Photos Edit more

Details

ID: 2617

NAME: 004-this

DESCRIPTION: by Don Sagrott - Understanding the 'this' keyword in javascript

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: 0

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and