splice 参数问题

splice 在 ie 与 firefox,chrome之间表现不太一致,希望大家以后也小心一点,今天遇到记录一下:

 

[1,2,3].splice(1)

 

ie下运行后变为 [1,2,3] ,相当于

 

[1,2,3].splice(1,0)

 

firefox ,chrome运行后变为 [1],相当于

 

[1,2,3].splice(1,[1,2,3].length-1)

 

这就涉及到了 splice第二个参数的默认值问题,看看 ECMA-262-5th 规范


Array.prototype.splice (start, deleteCount [ , item1 [ , item2 [ , … ] ] ] )


The length property of the splice method is 2.


证明 splice 函数申明有两个形参 start 与 deleteCount ,当调用者忽略deleteCount则deleteCount为undefined,那么看一下具体对于deleteCount的处理:


Let actualDeleteCount be min(max(ToInteger(deleteCount),0), len – actualStart).


而 ToInteger又依赖调用了 ToNumber


1.Let number be the result of calling ToNumber on the input argument.
2.If number is NaN, return +0.
3.If number is +0, −0, +∞, or −∞, return number.
4.Return the result of computing sign(number) * floor(abs(number)).


最后 ToNumber则规定 当遇到Undefined时返回NaN,那么ToInteger返回0,那么最后 actualDeleteCount为 0


则 IE表现为符合标准的正确行为,不知道为何 firefox,chrome没有遵从标准.



ps: 在 OReilly.JavaScript.The.Definitive.Guide.5th.Edition 中支持firefox,chrome的做法:


array.splice(start, deleteCount, value, ...)


deleteCount

The number of elements, starting with and including start, to be deleted from array. This argument is optional; if not specified, splice( ) deletes all elements from start to the end of the array.

不解

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值