问题描述
我有一个数组,我希望把它作为一个函数,如参数:
函数的东西(arrayP){
对于(VAR I = 0; I< arrayP.length;我++){
警报(arrayP [I]。价值);
}
}
我收到arrayP [0]是不确定的,因为里面的功能我从来没有写过arrayP是什么类型的数组这可能是真实的。因此,
- 是可以传递数组作为参数?
- 如果是这样,这是函数内部的要求是什么?
只需删除 .value的
,就像这样:
功能(arrayP){
对于(VAR I = 0; I< arrayP.length;我++){
警报(arrayP [I]); //这里没有。价值
}
}
当然,你可以传递一个阵列,但在该位置获得元素,使用的只有的 arrayName中的[索引]
的 .value的
将获得值
属性关闭对象的数组中的位置 - 这对于像字符串,数字等不存在。例如,的myString.value的
也将是未定义
。
I have an array, and I want to pass it as a parameter in a function such as:
function something(arrayP){
for(var i = 0; i < arrayP.length; i++){
alert(arrayP[i].value);
}
}
I'm getting that arrayP[0] is undefined, which might be true as inside the function I never wrote what kind of array arrayP is. So,
- Is is possible to pass arrays as parameters?
- If so, which are the requirements inside the function?
Just remove the .value
, like this:
function(arrayP){
for(var i = 0; i < arrayP.length; i++){
alert(arrayP[i]); //no .value here
}
}
Sure you can pass an array, but to get the element at that position, use only arrayName[index]
, the .value
would be getting the value
property off an object at that position in the array - which for things like strings, numbers, etc doesn't exist. For example, "myString".value
would also be undefined
.
这篇关于传递一个数组在JavaScript参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!