为什么在Chrome中存储到变量“name”的数组转换为字符串?

时间:2015-07-24 03:45:39

标签: javascript arrays google-chrome

当我在Chrome控制台中运行var name = [1,2,3]然后访问name的值时,我会回来"1,2,3"。为什么会这样?

enter image description here

1 个答案:

答案 0 :(得分:7)

您所看到的是一个全局变量,它是window.name对象的一部分。这实际上是浏览器使用的反映窗口名称的值。 (见documentation

由于var name是字符串getter / setter,因此您的数组将被强制转换为字符串。 (并且控制台在&#34;全局范围&#34;中运行,因此window.name<html> <head> <meta charset="utf-8"> <title>Sign Up For Beta Form</title> <link rel="stylesheet" href="css/style.css"> <link href='http://fonts.googleapis.com/css?family=Lato:400,700' rel='stylesheet' type='text/css'> <style> * { margin: 0; padding: 0; } body { width: 100%; font-family: 'Cabin', sans-serif; } p{margin-left:12px;} .wrapper { margin:auto; height: 1500px; display: block; width: 70%; -webkit-flex: 3 1 60%; flex: 3 1 60%; -webkit-order: 2; order: 2; } #right { background: blue; position:relative; display:inline-block; width: 30%; margin-left:auto; margin-right:auto; height: 500px; -webkit-flex: 1 6 20%; flex: 1 6 20%; -webkit-order: 3; order: 3; } #left { background: #333; position:relative; display:inline-block; margin-left:auto; margin-right:auto; width: 67%; height: 500px; -webkit-flex: 1 6 20%; flex: 1 6 20%; -webkit-order: 3; order: 3; } @media screen and (max-width: 700px) { #right, #left{display:block!important; width:100%;} .wrapper { display:block; width:80% -webkit-flex-flow: column; flex-direction: column; } #right, #left { /* Return them to document order */ -webkit-order: 0; order: 0; } #right{ min-height: 50px; max-height: 50px; } } </style> </head> <body> <div class="wrapper"> <div id="left"><p>left section<p></div> <div id="right"><p>Right Section<p></div> </div> </body> </html> 是相同的值。(如果您嵌套在函数内,这种相同的行为将不适用,因为它不再是全球范围了)