不能将数组分配给全局变量"名" [英] Can't assign an array to the global variable "name"

查看:104
本文介绍了不能将数组分配给全局变量"名"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经非常令人沮丧,尤其是考虑到它的简单性。简单的JavaScript:

This has been incredibly frustrating, especially given the simplicity of it. Simple javascript:

<script type="text/javascript">

var name = new Array("cat","dog");

document.write(name[1]);

</script>

该脚本会显示:A(如A,从猫......又名名[0] [1] ...)。

This script prints: "a" (as in the "a" from "cat"...aka name[0][1]...).

然而,当我改变它使用document.write(名称),它打印整个数组(即猫,狗)。为什么上帝的爱是这样简单数组未能打印整个字符串(应该是狗)?!

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")?!

推荐答案

您是在分配给window.name,它已经存在,并且具有的getter / setter函数做怪异/愚蠢的事情。

You are assigning it to window.name, which already exists and has getter/setter functions that do weird/stupid things.

如果你把它叫做别的东西,比如仅仅是N,它会如预期做。

If you call it something else, for instance just "n", it will do as expected.

和没有,克里斯,[]符号不有所作为,试了一下。

And no, Chris, [] notation doesn't make a difference, tried it.

这篇关于不能将数组分配给全局变量&QUOT;名&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆