非常恼人的JavaScript数组/对象错误 [英] Extremely annoying JavaScript array/object error
问题描述
我的Web应用程序之一,我基本上是重写的部分。我有一个脚本,将立即崩溃的接口部分或全部的面板,另一个code它们。
Basically, I am rewriting part of one of my web applications. I had a script that would collapse some or all panels of the interface at once, and another to code them.
不过,我的老功能看起来很丑陋,烦人键入并不够强大:
However, my old functions looked really ugly, and were annoying to type and not powerful enough:
function collapse_all()
{
document.getElementById("panel_1").style.display="none"
document.getElementById("panel_2").style.display="none"
document.getElementById("panel_3").style.display="none"
}
function expand_all()
{
document.getElementById("panel_1").style.display=""
document.getElementById("panel_2").style.display=""
document.getElementById("panel_3").style.display=""
}
现在我有这样的:
function panel() //first variable in argument is collapse or expand, all others are panels to act on
{
var panels = panel.arguments
alert(typeof panel.arguments)
var mode = panels.shift() //here's my problem
if(mode=="collapse") {mode="none"}
if(mode=="expand") {mode=""}
var items = panels.length
for (i = 0;i < items;i++) {document.getElementById(panels[i]).style.display=mode}
}
panel("collapse","panel_1","panel_2","panel_3")
我有一个问题,虽然。萤火告诉我panels.shift()不是一个函数。一些谷歌搜索我设法找出panel.arguments不是一个数组而是一个对象,所以我不能用它阵列的方法。我真的很困惑,我怎么既可以将对象转换成一个数组或另寻解决办法,因为我知道旁边没有关于JavaScript对象。一些示例code将是非常美联社preciated。
I have a problem though. Firebug tells me panels.shift() is not a function. With some Googling I managed to find out that panel.arguments isn't an array but an object, so I can't use array methods on it. I'm just really confused as to how I could either convert the object into an array or find another workaround, as I know next to nothing about JavaScript objects. Some example code would be highly appreciated.
推荐答案
您可以将arguments对象到像这样的数组:
You can convert the arguments object into an array like this:
var argsArray = Array.prototype.slice.call(arguments);
这样做是通过 Array.prototype
使用片
共同所有的阵列方法来创建一个真正的<从阵列状参数code>阵列
对象。 ()调用
(所有功能的方法)来调用这个片
方法以的
价值,它具有复制所有参数
并没有参数此这个$的元素的效果C $ C>进入一个新的数组。这看似迂回或哈克,但它实际上是设计到语言:看到的注的 ECMAScript的第三版规范第15.4.4.10。
What this does is use the slice
method common to all arrays via Array.prototype
to create a genuine Array
object from the array-like arguments
. call()
(a method of all functions) is used to call this slice
method with a this
value of arguments
and no parameters, which has the effect of copying all of the elements of this
into a new array. This may seem devious or hacky but it is actually designed into the language: see the note at the bottom of section 15.4.4.10 of the ECMAScript 3rd Edition spec.
此外,在函数中你所提供的参数
对象作为一个变量,所以你并不需要访问它作为函数对象的属性,你是这样做。在你的情况,只需使用参数
,而不是 panel.arguments
。
Also, within a function you are provided the arguments
object as a variable, so you don't need to access it as a property of the function object as you are doing. In your case, just use arguments
rather than panel.arguments
.
这篇关于非常恼人的JavaScript数组/对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!