我寫了jquery自定義fn擴展腳本來刪除數組中的給定元素,但它的工作方式意外的方式,所以我想弄清楚如何使其不使用返回參考方式進行參考操作。使用jquery fn擴展刪除給定的元素擴展在參考方式不工作
$.fn.removeElement=function(e){
var l=this.length;
while(l--){
if(this[l]==e){
console.log(this);
this.splice(l,1);
console.log(this);
break;
} } }
var x=['a','b','c','d','e'];
console.log(x);
$(x).removeElement('c');
console.log(x);
結果就像下面,
> ["a", "b", "c", "d", "e"]
> ["a", "b", "d", "e"]
> ["a", "b", "d", "e"]
> ["a", "b", "c", "d", "e"] //???
我期望[ 「一」, 「B」, 「d」, 「e」 的]在第四行。
p.s.我不想'回報'
上面的例子進行了修改,以更精確地證明這個問題。 – jnhwkim 2012-02-05 13:57:31