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) }); } }