简体   繁体   English

在数组原型中使用array.splice

[英]Using array.splice inside Array prototype

Array.prototype.move = function(oldIndex, newIndex) {
    var val = this.splice(oldIndex, 1);
    this.splice(newIndex, 0, val[0]);
}

//Testing - Change array position
var testarray = [1, 2, 3, 4];
testarray.move(3, 0);
console.log(testarray);

This produces an error "this.splice is not a function" yet it returns the desired results. 这将产生错误“ this.splice不是函数”,但会返回所需的结果。 Why? 为什么?

Array.prototype.move = function(oldIndex, newIndex) {
    if(Object.prototype.toString.call(this) === '[object Array]') {
        if(oldIndex && typeof oldIndex == 'number' && newIndex && typeof newIndex == 'number') {
            if(newIndex > this.length) newIndex = this.length;
            this.splice(newIndex, 0, this.splice(oldIndex, 1)[0]);
        }
    }
};

For some reason, the function is being called by the called by the document on load (still haven't quite figured that one out). 由于某种原因,该函数正在被加载的文档所调用(尚未完全弄清楚)。 I added a few checks to verify that this = an array, and then also reset the new index to be equal to the total size if the supplied int was greater than the total length. 我添加了一些检查以验证此=数组,然后如果提供的int大于总长度,则还将新索引重置为等于总大小。 This solved the error issue I was having, and to me is the simplest way to move objects around in an array. 这解决了我遇到的错误问题,对我来说,这是在数组中移动对象的最简单方法。 As for why the function is being called onload must be something to do with my code. 至于为什么要调用该函数,onload必须与我的代码有关。

You don't need the placeholder variable- 您不需要占位符变量-

Array.prototype.move = function(oldIndex, newIndex) {
       this.splice(newIndex, 0, this.splice(oldIndex, 1)[0]);
}
var a=[1,2,3,4,9,5,6,7,8];
a.move(4,8);

a[8]
/*  returned value: (Number)
9
*/

Adding properties to built–in objects is not a good idea if your code must work in arbitrary environments. 如果您的代码必须在任意环境中运行,则向内置对象添加属性并不是一个好主意。 If you do extend such objects, you shouldn't use property names that are likely to be used by someone else doing the same or similar thing. 如果您确实扩展了此类对象,则不应使用可能被其他人执行相同或相似操作的属性名称。

There seems to be more than one way to "move" a member, what you seem to be doing can be better named as "swap", so: 似乎有多种“移动”成员的方法,您似乎正在做的事情可以更好地命名为“交换”,因此:

if (!Array.prototype.swap) {
  Array.prototype.swap = function(a, b) {
    var t = this[a];
    this[a] = this[b];
    this[b] = t; 
  }
}

I expect that simple re-assignment of values is more efficient than calling methods that need to create new arrays and modify the old one a number of times. 我希望简单的值重新分配比调用需要创建新数组并多次修改旧数组的方法更有效。 But that might be moot anyway. 但是无论如何这可能是没有意义的。 The above is certainly simpler to read and is fewer characters to type. 上面的内容显然更易于阅读,而且键入的字符更少。

Note also that the above is stable, array.swap(4,8) gives the same result as array.swap(8,4) . 还要注意,以上内容是稳定的, array.swap(4,8)的结果与array.swap(8,4)

If you want to make a robust function, you first need to work out what to do in cases where either index is greater than array.length, or if one doesn't exist, and so on. 如果要使函数健壮,则首先需要弄清楚索引大于array.length或不存在索引的情况下的处理方法,依此类推。 eg 例如

var a = [,,2]; // a has length 3
a.swap(0,2);

In the above, there are no members at 0 or 1, only at 2. So should the result be: 在上面,在0或1处没有成员,仅在2处。所以结果应该是:

a = [2]; // a has length 1

or should it be (which will be the result of the above): 还是应该这样(将是上述结果):

a = [2,,undefined]; // a has length 3

or 要么

a = [2,,,]; // a has length 3 (IE may think it's 4, but that's wrong)

Edit 编辑

Note that in the OP, the result of: 请注意,在OP中,以下结果:

var b = [,,2];
b.move(0,2);

is

alert(b); // [,2,];

which may not be what is expected, and 这可能不是预期的,并且

b.move(2,0);
alert(b); // [2,,];

so it is not stable either. 因此它也不是稳定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM