This has been incredibly frustrating, especially given the simplicity of it. Simple javascript:
这令人非常沮丧,特别是考虑到它的简单性。简单的javascript:
<script type="text/javascript">
var name = new Array("cat","dog");
document.write(name[1]);
</script>
This script prints: "a" (as in the "a" from "cat"...aka name[0][1]...).
这个脚本打印:“a”(如“cat”中的“a”...又名[0] [1] ......)。
Yet when I change it to document.write(name), it prints the whole array (i.e. "cat,dog"). Why for the love of god is this simple array failing to print the entire string (which should be "dog")?!
然而,当我将其更改为document.write(name)时,它会打印整个数组(即“cat,dog”)。为什么对于上帝的爱是这个简单的数组无法打印整个字符串(应该是“狗”)?!
2 个解决方案
#1
6
The global variable name
refers to the existing window.name
property, which was used to set the name of the browser window. It converts any value assigned to it to a string:
全局变量名称引用现有的window.name属性,该属性用于设置浏览器窗口的名称。它将分配给它的任何值转换为字符串:
window.name = [1, 2, 3];
console.log(typeof window.name, window.name);
If you change the variable name to e.g. names
it will do as expected.
如果您将变量名称更改为例如它将按预期执行的名称。
Using name
in function scope scope will work fine of course.
在函数范围范围内使用名称当然可以正常工作。
#2
-2
You should use this instead:
你应该使用它:
var name = ["cat", "dog"];
document.write(name[1]);
#1
6
The global variable name
refers to the existing window.name
property, which was used to set the name of the browser window. It converts any value assigned to it to a string:
全局变量名称引用现有的window.name属性,该属性用于设置浏览器窗口的名称。它将分配给它的任何值转换为字符串:
window.name = [1, 2, 3];
console.log(typeof window.name, window.name);
If you change the variable name to e.g. names
it will do as expected.
如果您将变量名称更改为例如它将按预期执行的名称。
Using name
in function scope scope will work fine of course.
在函数范围范围内使用名称当然可以正常工作。
#2
-2
You should use this instead:
你应该使用它:
var name = ["cat", "dog"];
document.write(name[1]);