Javascript array.join-未定义新变量
获取错误通知时说“未捕获引用错误:未定义joinedNames”无法清楚地获取您试图执行的操作。如果需要加入数组的名称, 参考这个Javascript array.join-未定义新变量,javascript,Javascript,获取错误通知时说“未捕获引用错误:未定义joinedNames”无法清楚地获取您试图执行的操作。如果需要加入数组的名称, 参考这个 函数sayHello(arr){ const joinedNames=arr.join('and'); 返回联合名称; } log(sayHello(['mary','john'])您需要调用sayHello而不是joinedNames。另外,在使用name1和name2时,不需要传递数组 function sayHello(name1, name2) { c
函数sayHello(arr){
const joinedNames=arr.join('and');
返回联合名称;
}
log(sayHello(['mary','john'])代码>您需要调用sayHello而不是joinedNames。另外,在使用name1和name2时,不需要传递数组
function sayHello(name1, name2) {
const joinedNames = sayHello.join(' and ');
return joinedNames();
}
console.log(joinedNames(['mary', 'john']));
以下是我认为您的脚本应该完成的工作版本:
function sayHello(name1, name2) {
return [name1, name2].join(' and ');
}
console.log(sayHello('mary', 'john'));
您可以使用以下常规功能:
function sayHello(name1, name2) {
const joinedNames = [name1, name2].join(' and '); //You want to join the provided names, not the function sayHello
return joinedNames; //joinedNames is a string, not a function, so you can't call it
}
console.log(sayHello('mary', 'john')); //You want to call the function you defined, joinedNames is just the variable defined within the function
这是一个有效且可伸缩的代码
const sayHello = (...names) => names.join(' and ');
sayHello('Mike', 'Laura', 'Jackie');
你想做什么?joinedNames
是在函数范围内定义的,在该函数之外是不可访问的。我猜你的意思是sayHello(['mary','john'])
和name1.join('and'))
变量joinedNames
在函数sayHello
的范围内,而不在控制台的范围内。谢谢!弄明白了。我很感激!非常感谢你!
function sayHello(name1, name2) {
var names = [name1, name2];
const joinedNames = [...names].join(' and ');
return joinedNames;
}
console.log(sayHello('mary', 'john'));