Javascript 回调函数不使用';世界js
我正在尝试创建一个回调函数。我使用的函数getPosts与getResult中的回调函数类似。GetPost创建数组,使用getResult函数。但当我调用一个函数时,getResult只执行getPosts函数Javascript 回调函数不使用';世界js,javascript,arrays,Javascript,Arrays,我正在尝试创建一个回调函数。我使用的函数getPosts与getResult中的回调函数类似。GetPost创建数组,使用getResult函数。但当我调用一个函数时,getResult只执行getPosts函数 function getPosts(index) { let arr = index.split(' '); console.log(arr);} function getResult (callback) { callback(index) let sh
function getPosts(index) {
let arr = index.split(' ');
console.log(arr);}
function getResult (callback) {
callback(index)
let show = arr[0]+arr[2]+arr[1];
console.log(show);
}
首先,您尝试使用
arr
,它是另一个函数中的本地数组,不能在其他地方使用。
我相信您希望使用getPosts()
函数作为getResult()
函数的参数:
希望此代码能帮助您:
function getPosts(index) {
let arr = index.split(' ');
console.log(arr);
return arr;
}
function getResult (callback) {
let show = callback[0]+callback[2]+callback[1];
console.log(show);
}
getPosts('my data here');
getResult(getPosts('my data here'));
首先,您尝试使用
arr
,它是另一个函数中的本地数组,不能在其他地方使用。
我相信您希望使用getPosts()
函数作为getResult()
函数的参数:
希望此代码能帮助您:
function getPosts(index) {
let arr = index.split(' ');
console.log(arr);
return arr;
}
function getResult (callback) {
let show = callback[0]+callback[2]+callback[1];
console.log(show);
}
getPosts('my data here');
getResult(getPosts('my data here'));
您在代码段中从未调用过
getResult
。在代码段中从未调用过getResult
。谢谢,这就是我想做的,这就是我想做的