Javascript outer scope variable access

后端 未结 1 637
栀梦
栀梦 2021-01-12 03:07
OperationSelector = function(selectElement) {
    this.selectElement = selectElement;
}

OperationSelector.prototype.populateSelectWithData = function(xmlData) {
            


        
相关标签:
1条回答
  • 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")));
        }); 
    }
    
    0 讨论(0)
提交回复
热议问题