Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  111] [ 2]  / answers: 1 / hits: 26596  / 13 Years ago, sun, february 5, 2012, 12:00:00
...
$.fn.annotateEdit = function(image, note) {
if (note) {
this.note = note;
} else {
var newNote = new Object();
newNote.id = new;
this.note = newNote;
}
}
...
var mynote = this.note;

form.find(':radio').change(function() {
var vacancy = $(this).attr('value');
mynote.vacancy = vacancy;
});
...


Is it possible to access this.note from the change() handler without defining mynote?


More From » jquery

 Answers
38

I use a pattern like this so I can access anything in the enclosing scope:



var that = this;
...

form.find(':radio').change(function () {
that.note.vacancy = $(this).attr('value');
});


I am a fan of this pattern because it makes the code a little more readable. In my opinion, it is clear what it being accessed is part of the enclosing scope (as long as the usage of that is consistent).


[#87641] Thursday, February 2, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckenna

Total Points: 445
Total Questions: 109
Total Answers: 109

Location: Virgin Islands (U.S.)
Member since Sun, May 16, 2021
3 Years ago
;