我想在这里得到一点指导。为此使用JS Bin。这里的整个问题是:“名称”用作 VAR 并在数组内部时:---console.log 打印每个字母:
var word = "Hi";
var name = ["John","Suzette","Mari-Louise","Tinus","Hendrik","Koos","Elna","Elbie"];
// Greeting
greeter(word,name);
function greeter(str,arr){
var counter;
for(counter = 0;
counter < arr.length;
counter++) {
console.log(str + " " + arr[counter]);
}
}
输出
"Hi J"
"Hi o"
"Hi h"
"Hi n"
"Hi ,"
"Hi S"
"Hi u"
但是,将 VAR 更改为 userName 会产生正确的结果,..我在 JS 中找不到任何对“name”作为保留字的引用,所以如果有人可以为我澄清这一点,那就太棒了。
var word = "Hi";
var userName = ["John","Suzette","Mari-Louise","Tinus","Hendrik","Koos","Elna","Elbie"];
// Greeting
greeter(word,userName);
function greeter(str,arr){
var counter;
for(counter = 0;
counter < arr.length;
counter++) {
console.log(str + " " + arr[counter]);
}
}
结果**
"Hi John"
"Hi Suzette"
"Hi Mari-Louise"
"Hi Tinus"
"Hi Hendrik"
"Hi Koos"
"Hi Elna"
"Hi Elbie"