简体   繁体   English

JavaScript中的多个和单个数组名称

[英]Plural and singular array names in JavaScript

I'm very confused by the behavior of this code in Chrome 59.0.3071.115: 我对Chrome 59.0.3071.115中此代码的行为感到非常困惑:

var names = ["Cat 1", "Cat 2"];
console.log(names);

Prints an array object, but 打印一个数组对象,但是

var name = ["Cat 1", "Cat 2"];
console.log(name);

Prints the string "Cat 1, Cat 2" . 打印字符串"Cat 1, Cat 2"

Why is this happening? 为什么会这样?

name is a predefined getter/setter property of the window object , and will always be a string data type. namewindow对象的预定义getter / setter属性 ,并且始终是字符串数据类型。 So when you assign to it, you are not assigning to a variable, but using a setter, which will convert the given value to a string. 因此,当您分配给它时,您没有分配给变量,而是使用setter,它将给定值转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM