javascript - How to correctly reference "this"? -


this question has answer here:

assuming have following:

var object = {     myfunc: function() {         $.ajax({            url: url,            format: format,            success: function() {             console.log(this) // refers ajax call , not "object"                  $.ajax({                   url: url,                   format: format,                   success: function() {                     console.log(this) // refers nested ajax call , not "object"                   }                 });              }         });     } } 

how "this" reference "object" opposed ajax call?

use $.proxy() pass custom context callback function

var object = {     myvar : "hello",     myfunc : function() {         $.ajax({             url : url,             format : format,             success : $.proxy(function() {                  console.log(this) // refers ajax                 // call ,                 // not "object"                  $.ajax({                     url : url,                     format : format,                     success : function() {                         console.log(this) //                         // refers                         // nested ajax call                         // , not "object"                     }                 });              }, this)         });     } } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -