Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have it right that the 'this' keyword which refers to the object on which the currently executing method has been invoked. So in your first example the <code>clickHandler()</code> function is going to refer to the user object. </p> <p>Now in terms of jQuery when you are inside of a callback function 'this' is referring to the the 'DOM' element. The reason for this as i understand is that jQuery returns a object from its internal 'jQuery' code that maintains the reference to the element that was in context being the 'DOM' element using call() and apply(). I believe to maintain it. Doing so also allows you to accomplish that chaining of actions like <code>("button").click(somefunction).fadeIn()</code> for example.</p> <p>If you make your own jquery function such as <code>$.fn.somefunction = function() {...}</code> this refers to a jQuery object at this point.</p> <p>There might be a better why to accomplish this but I quickly changed your code using <code>call()</code> to make is refer to your user object.</p> <pre><code>var user = { tournament:"The Masters", data :[ {name:"T. Woods", age:37}, {name:"P. Mickelson", age:43} ], clickHandler: function () { // the use of this.data here is fine, because "this" refers to the user object, // and data is a property on the user object. this.data.forEach (function (person) { // But here inside the anonymous function (that we pass to the forEach method), //"this" no longer refers to the user object. // This inner function cannot access the outer function's "this" //Use call to make this refer to your user object that = Object.call(this, user); console.log ("What is This referring to? " + that); //[object Object] console.log (person.name + " is playing at " + that.tournament); // T. Woods is playing at undefined // P. Mickelson is playing at undefined }) } } user.clickHandler(); // What is This referring to? [object Object] </code></pre> <p>Another thing is in Javascript the <code>forEach</code> function takes a second parameter that will be used as the object to reference for 'this' so another way you could have done it. Now this refers to user object.</p> <pre><code>.... this.data.forEach (function (person) { // But here inside the anonymous function (that we pass to the forEach method), //"this" no longer refers to the user object. // This inner function cannot access the outer function's "this" //Use call to make this refer to your user object console.log ("What is This referring to? " + this); //[object Object] console.log (person.name + " is playing at " + this.tournament); // T. Woods is playing at Masters // P. Mickelson is playing at Masters //pass user as the object the second parameter }, user) } } </code></pre> <p><strong>Check out the explanation of this on the jquery site here is a link.</strong></p> <p><a href="http://learn.jquery.com/javascript-101/this-keyword/" rel="nofollow">http://learn.jquery.com/javascript-101/this-keyword/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload