简体   繁体   中英

Plural and singular array names in JavaScript

I'm very confused by the behavior of this code in 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" .

Why is this happening?

name is a predefined getter/setter property of the window object , and will always be a string data type. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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