【发布时间】:2013-07-07 07:21:03
【问题描述】:
偶然发现了这段代码,让我摸不着头脑,想知道他们抓住长度来设置索引有什么好处。
var thisarray = new Array();
function addtoArray(int){
thisarray[thisarray.length] = int;
}
通过数组.push
function addtoArray(int){
thisarray.push(int)
}
另外,有没有这个php的等价物
thisarray[]
【问题讨论】:
-
是的,
thisarray[thisarray.length] = something等价于 PHP 的thisarray[] = something。而push()等价于array_push()。 -
Javascript 的推送不“等同于”PHP 的 array_push。 Javascript 的 push 是一个对象/实例方法;它使用点符号直接在数组上调用。使用 PHP 的 array_push,您必须将数组作为参数传递。
-
根据我的经验,不使用 push 表明开发人员已经使用 Javascript 很长时间了,因为 before push 被引入/完全支持 -- 我记得当它不是一个选项时,回到 1998/99 年左右。
标签: javascript