array.splice

来源:互联网 发布:win7优化内存 编辑:程序博客网 时间:2024/06/05 03:02
原文@http://www.coshima.com/2011/02/02/array-splice-in-internet-explorer/

array.splice() in Internet Explorer

I finally got around to investigating an IE bug I’ve been meaning to look into. The root cause was a difference between JScript and ECMAScript as regards array.splice(). The signature for this handy method that removes and inserts array elements in place:

splice(startdeleteCountvalue, ...)

According to the standard, only start (the index at which to start removing elements) is a required argument. So:

var a = ["e1", "e2", "e3"];
a.splice(1);

should result in a being a single-element array containing “e1″. But of course, IE does not see it that way. According to MSDN, deleteCount is also required argument. Thus, the above would have to be:

var a = ["e1", "e2", "e3"];
a.splice(1, a.length-1);

It didn’t take too long to figure this out, but Google let me down and didn’t turn up anything about this. It’s been a long time since I had to turn to the MSDN reference docs. Maybe this tip will help someone…at the very least, it’ll help me remember!