gpt4 book ai didi

Javascript外部作用域变量访问

转载 作者:数据小太阳 更新时间:2023-10-29 04:07:35 26 4
gpt4 key购买 nike

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>');
});
}

如何在迭代 block 中访问 OperationSelector.selectElement?

最佳答案

在迭代函数之前将它分配给函数作用域中的局部变量。然后你可以在里面引用它:

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")));
});
}

关于Javascript外部作用域变量访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2274162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com