为什么splice方法返回undefined,并且不删除以下代码中位置4处的元素:
var excludedDepartmentsList = [1, 2, 3, 4, 5, 6];
var currentDepartmentId = 5;
var position = $.inArray(currentDepartmentId, excludedDepartmentsList);
if (position > -1) {
var q = excludedDepartmentsList.splice[position, 1];
return;
}我在这里做了一个测试:http://jsfiddle.net/PnVEb/
发布于 2013-06-08 00:56:36
.splice是一个函数,应该像调用excludedDepartmentsList.splice(position, 1)而不是excludedDepartmentsList.splice[position, 1]那样调用它。请注意,括号从[]更改为()。
使用如下所示的(),它应该返回5
excludedDepartmentsList.splice(position, 1)固定小提琴: http://jsfiddle.net/PnVEb/1/
发布于 2013-06-08 00:57:43
[]不等于()
var q = excludedDepartmentsList.splice[position, 1];
^ ^发布于 2013-06-08 00:58:03
你的语法有点错误。您可以为拼接函数键入[],但它应该是()。
在大多数编程语言中,[]用于数组,()也用于函数/方法。
此外,您还必须返回q
if (position > -1) {
var q = excludedDepartmentsList.splice(position, 1);
return q;
}https://stackoverflow.com/questions/16989293
复制相似问题