本文介绍了Javascript外部范围变量访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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>');
});
}
如何在迭代块中访问OperationSelector.selectElement?
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")));
});
}
这篇关于Javascript外部范围变量访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!