When I declare OnSuccess inside another function like below, the browser developer tools console indicates an error that there's no function called OnSuccess in the global scope. From this, I infer that any functions declared like below inside another function are still hoisted/pinned/treated as functions of the HTML DOM window object.
Then, I declare OnError like you declare a member inside an object initializer. That doesn't work either.
One way I could do is move both OnError and OnSuccess outside the AssignBackupOnServer function, but I want the OnSuccess and OnError to be able to look at the local variables of the AssignBackupOnServer, so I have declared it within the AssignBackupOnServer function.
What's the right way of going about this declaration if I want the contained lambda to exhibit a closure on the captured variables of its containing/enclosing function?
function AssignBackupOnServer(mainGuyUId, backupUId, backupFirstName, backupLastName) {
var url = '/Foo/SearchForBackup';
$.ajax(url,
{
cache: false,
async: false,
type: 'POST',
data: JSON.stringify(
{
'mainGuyUId': mainGuyUId,
'backupUId': backupUId,
'backupFirstName': backupFirstName,
'backupLastName': backupLastName
}),
dataType: 'json',
contentType: 'application/json',
success: AssignBackupOnServer.OnSuccess,
error: AssignBackupOnServer.OnError
});
function OnSuccess(data, textStatus, jqXHR) {
// I want to use mainGuyUId, backupUId,
// backupFirstName, and backupLastName
// here and therefore, I want this function
// to exhibit a closure on its containing lambda
// so I have declared it here
}
OnError : function(jqXHR, textStatus, errorThrown) {
}
}