简体   繁体   English

JavaScript中字符串数组的长度

[英]Length of an Array of String in JavaScript

In the following JavaScript code snippet, I expect that the console output be 3 but in both Chrome and Mozilla, it is 23. Why? 在以下JavaScript代码段中,我希望console输出为3但是在Chrome和Mozilla中, console输出均为23。为什么?

var status = ["Busy", "Preferred", "Available"];        
console.log( status.length);

There's already a window.status (a global variable named status ). 已经有一个window.status (一个名为status的全局变量)。 Read more here . 在这里阅读更多。

When you're trying to set the array ["Busy", "Preferred", "Available"] to your global variable, the window.status setter will be invoked. 当您尝试将数组["Busy", "Preferred", "Available"]为全局变量时,将调用window.status setter。 So, window.status property will contain the string "Busy,Preferred,Available" . 因此, window.status属性将包含字符串"Busy,Preferred,Available"

So, yeah change the variable name or don't use global variables (please). 因此,请更改变量名称或不使用全局变量(请)。

看起来status是Chrome和Firefox的保留变量,请尝试使用其他变量名。

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

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