splice 是 es3 就有的东西哦
我想问要是自己写,该怎么写
@月下大庚角:
Array.prototype.splice2 = function ( offset, remove/*, data... */ ) {
var data, begin, removed, end;
data = Array.prototype.slice.call( arguments, 2 );
begin = this.slice( 0, offset );
removed = this.slice( offset, remove );
end = this.slice( offset + remove );
this.length = 0;
// This polyfill only been discovered to be necessary on Opera
// and it seems to handle up to 1048575 function parameters.
this.push.apply( this, begin );
this.push.apply( this, data );
this.push.apply( this, end );
return removed;
};
origin: https://gerrit.wikimedia.org/r/c/mediawiki/extensions/VisualEditor/+/72233/6/modules/ve/ve.js#406
想怎么实现就找 polyfill
@huiyuanai709: 多谢,请问这个 polyfill 怎么用...
@月下大庚角:
上边这个 removed 应该有点问题
换成这样
removed = this.slice( offset, remove + offset);
原型方法加上后
var a = [1, 2, 3, 4, 5]
a.splice(1, 2, 6, 7);
换成
a.splice2(1, 2, 6, 7)