javascript IE <= 8 .splice() 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8332969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE <= 8 .splice() not working
提问by Dave Stein
I have some simple code you can see in my fiddle. It alerts properly in all browsers and IE9, but not IE8 or 7.
我有一些简单的代码,你可以在我的 fiddle 中看到。它在所有浏览器和 IE9 中都能正常发出警报,但不适用于 IE8 或 7。
var func = function( x ) {
var slice = [].slice,
args = slice.call( arguments ),
pass = args.splice(1);
alert( pass );
};
func( 'a', 1, 2 );
EDITUsing the solution I posted what I used here: http://jsfiddle.net/7kXxX/4/
编辑使用我在这里发布的解决方案:http: //jsfiddle.net/7kXxX/4/
I am using this in a case where I don't know how many arguments are coming, which is why I'm using "arguments"
我在不知道有多少参数即将到来的情况下使用它,这就是我使用“参数”的原因
回答by Wayne
The ECMAScript 3rd edition standardrequires the second deleteCount
argument:
在ECMAScript的第三版标准要求的第二个deleteCount
参数:
Array.prototype.splice(start, deleteCount [, item1 [, item2[,...]]])
MSDN docsshow that IE follows this standard:
MSDN 文档显示 IE 遵循此标准:
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
Firefox's SpiderMonkey allows the second argument to be optional(as do other modern browsers):
Firefox 的 SpiderMonkey 允许第二个参数是可选的(其他现代浏览器也是如此):
array.splice(index , howMany[, element1[, ...[, elementN]]])
array.splice(index[, howMany[, element1[, ...[, elementN]]]])
Description:
描述:
howManyAn integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.
howMany一个整数,指示要删除的旧数组元素的数量。如果 howMany 为 0,则不删除任何元素。在这种情况下,您应该至少指定一个新元素。如果没有指定 howMany 参数(上面的第二种语法,它是 SpiderMonkey 扩展),索引之后的所有元素都将被删除。
Sources:
资料来源:
回答by Esailija
Splice has a required second argument:
Splice 有一个必需的第二个参数:
pass = args.splice(1,2);
The optional second argument is an extension in newer browsers that assume the rest of the array if left undefined
可选的第二个参数是较新浏览器中的扩展,如果未定义,则假定数组的其余部分
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice
Slice would be more appropriate if you want elements from 1 - to the end, doesn't look like there is any reason to remove elements from args.
如果您想要从 1 到结尾的元素,切片会更合适,看起来没有任何理由从 args 中删除元素。
回答by Chris
My solution to the Array.prototype.splice in IE (read more here):
我在 IE 中对 Array.prototype.splice 的解决方案(在这里阅读更多):
(function () {
'use strict';
var originalSplice = Array.prototype.splice;
Array.prototype.splice = function (start, deleteCount) {
// convert the weird, not-really-an-array arguments array to a real one
var args = Array.prototype.slice.call(arguments);
// IE requires deleteCount; set default value if it doesn't exist
if (deleteCount === undefined) {
args[1] = this.length - start;
}
// call the original function with the patched arguments
return originalSplice.apply(this, args);
};
}());
回答by user2305421
var func = function (x)
{
alert ([].slice.call (arguments, 1))
}
func( 'a', 1, 2 );