我发现t his function on the john resig blog用于从阵列中删除元素。它工作得很好!但我真的不知道如何..了解阵列删除功能
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
我感到困惑的是与这一说法发生的事情:(to || from) + 1 || this.length)
对于初学者;也许一旦我明白了,其余的就会变得更加清晰。任何帮助明确发生在这里的事情是非常感谢。谢谢。
一旦您了解了函数的行为方式,就不难看出发生了什么。该行后面的逻辑在文章底部解释,顺便说一句.. – 2012-08-06 15:15:09
为什么不使用'.splice'来代替? – Ryan 2012-08-06 15:21:20
@TimDown你能否提出另一个相同的功能,但也许会使它更清晰? – 1252748 2012-08-06 15:21:48