我目前正在尝试理解Node和回调,并尝试了各种解决方案以使其工作,但是param2未定义。谁能告诉我为什么?以及如何解决这个问题?谢谢!
function getPage(callback) {
url = 'http://www.google.com';
if (url) {
url = url;
} else {
console.log('There was an error. No URL submitted');
}
callback(url, param2);
}
function CB(url, param2) {
console.log(`The URL of the page requested was ${url} and the added argument was ${param2}`);
}
getPage(CB);
答案 0 :(得分:2)
这是因为param2
在您调用回调函数的范围内未定义。要定位param2
,请确保在param2
getpage()
function getPage(callback) {
url = 'http://www.google.com';
// **make sure param2 is defined**
var param2 = "param2 value"
if (url) {
url = url;
} else {
console.log('There was an error. No URL submitted');
}
callback(url, param2);
}
function CB(url, param2) {
console.log(`The URL of the page requested was ${url} and the added argument was ${param2}`);
}
getPage(CB);
答案 1 :(得分:1)
在getPage函数中,定义param2并将其传递给