在数组中使用名称在 Firefox 中会产生不同的结果

Using name inside a Array yields different results in Firefox

我想在这里得到一些指导。为此使用 JS Bin。 这里的整个问题是: "name" 当用作 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 会产生正确的结果,..我找不到任何对 'name' 的引用是 JS 中的保留字,所以如果有人可以为我澄清一下,这将是粉碎的。

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"

哦,但是 'name' 已保留:

http://www.w3schools.com/js/js_reserved.asp

在 JavaScript 对象、属性和方法下查看

如果调试,您会注意到 name 在您执行代码时已经定义。发生这种情况是因为全局 window 上下文有 name 属性 这是字符串。
当您尝试将 ["a", "b", "c"] 设置为此 属性 时,浏览器会将其转换为字符串,并变为 "a,b,c"。这就是为什么当你遍历它时,你会得到字符。

console.log(name); // it already exists

var name = ["a", "b", "c"]; // assigns window.name property, becomes a string
var nameqwe = ["a", "b", "c"]; // creates local variable

console.log(name);
console.log(nameqwe);