[英]passing variables from function as argument
我试图传递一个保存为var的数组。 我在父函数中声明var并将arr作为参数添加到父函数中。 然后,我在回调调用中将arr作为参数引入。 控制台告诉我,linksA是未定义的。
var supportLists = function(selector, arr) {
var parentList = document.getElementById(selector);
var theList = parentList.querySelectorAll("a");
var linksA = [
"http://www.example.com",
"http://www.example.com/path2",
"1",
"2",
"4",
"3",
"5",
"6",
"7"
];
var linksB = [
"1",
"2",
"3"
];
var linksC = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12"
];
var linksD = [
"1",
"2"
];
var linksE = [
"1",
"2",
"3"
];
var linksF = [
"1",
"2",
"3",
"4",
"5",
"6"
];
var linksG = [
"1",
"2",
"3"
];
var linksH = [
"1",
"2",
"3",
"4"
];
var linksI = [
"1"
];
var linksJ = [
"1",
"2",
"3",
"4",
"5"
];
function processLi(listItems, links) {
for (var i = 0; i < links.length; i++) {
listItems.forEach(function(item, index) {
item.href = links[index];
item.target = "_blank";
});
}
}
processLi(theList, arr);
};
supportLists("support-list", linksA);
supportLists("support-list-b", linksB);
supportLists("support-list-c", linksC);
supportLists("support-list-d", linksD);
supportLists("support-list-e", linksE);
supportLists("support-list-f", linksF);
supportLists("support-list-g", linksG);
supportLists("support-list-h", linksH);
supportLists("support-list-i", linksI);
supportLists("support-list-j", linksJ);
如果要使用变量,则必须在要使用它或将其传递给函数的范围中使用它,这意味着必须在该范围或父范围中声明它。
由于要将数组传递给supportLists
函数,因此必须在该函数之外声明它们。
如果将所有数组声明移到函数外部,则代码将类似于以下内容(我添加了一些注释以显示作用域的起始位置和终止位置)
// This is the 'parent' scope (probably the global/window scope in your case)
var linksA = [
"http://www.example.com",
// ...
];
// ...
var linksJ = [
"1",
"2",
"3",
"4",
"5"
];
var supportLists = function(selector, arr) {
// Here begins the 'supportLists' function scope
// The 'supportLists' function has access to this scope and the 'parent' scope
var parentList = document.getElementById(selector);
var theList = parentList.querySelectorAll("a");
function processLi(listItems, links) {
// Here begins the 'processLi' function scope
// The 'processLi' function has access to this scope, the 'supportLists' scope and the 'parent' scope
for (var i = 0; i < links.length; i++) {
listItems.forEach(function(item, index) {
// Here begins the 'function(item, index)' function scope
// The 'function(item, index)' function has access to this scope, the 'processLi' scope, the 'supportLists' scope and the 'parent' scope
item.href = links[index];
item.target = "_blank";
});// Here ends 'function(item, index)' function scope
// Back in the 'processLi' function scope
}
} // Here ends the 'processLi' function scope
// Back in the 'supportLists' function scope
processLi(theList, arr);
}; // Here ends the 'supportLists' function scope
// Back in the 'parent' scope
supportLists("support-list", linksA);
supportLists("support-list-b", linksB);
supportLists("support-list-c", linksC);
supportLists("support-list-d", linksD);
supportLists("support-list-e", linksE);
supportLists("support-list-f", linksF);
supportLists("support-list-g", linksG);
supportLists("support-list-h", linksH);
supportLists("support-list-i", linksI);
supportLists("support-list-j", linksJ);
目前,您的变量是在本地范围内定义的,因此,它们对于supportLists
函数外部的代码不可见。
解:
在函数外定义变量。 例,
var linksB = ["1", "2", "3"];
var supportLists = function(selector, arr) { //YOUR CODE }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.