Skip to content Skip to sidebar Skip to footer

What Does "this" Refer To In The Following Javascript?

DISCLAIMER: I am asking about a specific use of this, not what this is used for in general. So, please no google/copy/paste speed answers (: I have the javascript/jquery code below

Solution 1:

According to jQuery.ajax documentation:

The this reference within all callbacks is the object in the context option passed to $.ajax in the settings; if context is not specified, this is a reference to the Ajax settings themselves.

In other words, since you didn't set the context option, this will be the options object {...} passed as the parameter to $.ajax.

The code you posted seems wrong: it reads fromPage from the wrong object. It would work if you set fromPage on the options object instead:

req = $.ajax({
    //other options here...fromPage: fromPage
});

Solution 2:

onSuccess() is being called from the complete handler in the context of the ajax request, which is being assigned to the req object, so this is that context - i.e. the req object, and fromPage is in fact req.fromPage

Solution 3:

I belive this is refering to req.fromPage since that is the object that contains the called function.

Post a Comment for "What Does "this" Refer To In The Following Javascript?"