开发者

Javascript outer scope variable access

开发者 https://www.devze.com 2022-12-20 13:34 出处:网络
OperationSelector = function(selectElement) { this.selectElement = selectElement; } OperationSelector.prototype.populateSelectWithData = function(xmlData) {
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 ?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号