Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript outer scope variable access

OperationSelector = function(selectElement) {
    this.selectElement = selectElement;
}

OperationSelector.prototype.populateSelectWithData = function(xmlData) {
    $(xmlData).find('operation').each(function() {
        var operation = $(this);
        selectElement.append('<option>' + operation.attr("title") + '</option>');               
    });
}

How could I access OperationSelector.selectElement in iteration block ?

like image 399
dmitrynikolaev Avatar asked Feb 16 '10 15:02

dmitrynikolaev


1 Answers

Assign it to a local variable in the function scope before your iteration function. Then you can reference it within:

OperationSelector = function(selectElement) { 
    this.selectElement = selectElement; 
} 

OperationSelector.prototype.populateSelectWithData = function(xmlData) { 
    var os = this;
    $(xmlData).find('operation').each(function() { 
        var operation = $(this); 
        os.selectElement.append(new Option(operation.attr("title")));
    }); 
}
like image 178
Andy E Avatar answered Nov 07 '22 07:11

Andy E