带返回值的调用函数仅返回不带参数的字符串。它仅输出“嗨,我是”,我在这里缺少什么。我搜寻了...
var string = function nameString(name) {
return "Hi, I am" + " " +name;
};
string("casey");
console.log(nameString(name));
我也这样写。
function nameString(name){
return "Hi, I am" + " " + name ;
};
nameString("casey");
console.log(nameString(name));
最佳答案
您不会使用已定义的参数来调用它,因此它不会做任何事情。
分解您的示例:
var string = function nameString(name) { // Declares a function nameString and assigns it to string
return "Hi, I am" + " " +name;
};
string("casey"); // Calls string with name:"casey" but discards the return value
console.log(nameString(name)); // Calls nameString with the undefined global variable name
那将行不通,因为您最终返回了
"Hi, I am " + undefined
。您可能想要做的是调用nameString,然后保存返回值,该返回值写为:
function nameString(name) {
return "Hi, I am" + " " +name;
};
var string = nameString("casey");
console.log(string );