Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何扩展Array.prototype.push()?_Javascript_Arrays_Prototype - Fatal编程技术网

Javascript 如何扩展Array.prototype.push()?

Javascript 如何扩展Array.prototype.push()?,javascript,arrays,prototype,Javascript,Arrays,Prototype,我正在尝试扩展Array.push方法,以便使用push将触发回调方法,然后执行普通的数组函数 我不太确定如何做到这一点,但这里有一些代码我一直在玩,但没有成功 arr = []; arr.push = function(data){ //callback method goes here this = Array.push(data); return this.length; } arr.push('test'); 你可以这样做: arr = [] arr.p

我正在尝试扩展Array.push方法,以便使用push将触发回调方法,然后执行普通的数组函数

我不太确定如何做到这一点,但这里有一些代码我一直在玩,但没有成功

arr = [];
arr.push = function(data){

    //callback method goes here

    this = Array.push(data);
    return this.length;

}

arr.push('test');

你可以这样做:

arr = []
arr.push = function(data) {
  alert(data); //callback

  return Array.prototype.push.call(this, data);
}
如果您遇到没有电话的情况,您也可以选择以下解决方案:

arr.push = function(data) {
  alert(data); //callback

  //While unlikely, someone may be using psh to store something important
  //So we save it.
  var saved = this.psh;
  this.psh = Array.prototype.push;
  var ret = this.psh(data);
  this.psh = saved;
  return ret;
}
编辑:

在我告诉您如何做的同时,最好使用另一种方法来执行回调,然后只调用数组上的push,而不是重写push。你可能会有一些意想不到的副作用。例如,push看起来是varadic(接受可变数量的参数,如printf),使用上面的命令将打破这一点

您需要处理_Arguments()和_ArgumentsLength()以正确重写此函数。我强烈建议反对这条路线

再次编辑:
或者你可以用“论点”,那也行。但是仍然建议不要采用这种方法。

因为push允许推送多个元素,所以我使用下面的arguments变量让real push方法拥有所有参数

此解决方案仅影响arr变量:

arr.push = function (){
    //Do what you want here...
    return Array.prototype.push.apply(this,arguments);
}
此解决方案影响所有阵列。我不建议你那样做

Array.prototype.push=(function(){
    var original = Array.prototype.push;
    return function() {
        //Do what you want here.
        return original.apply(this,arguments);
    };
})();

JavaScript 1.2中引入了Array.prototype.push。其实就这么简单:

Array.prototype.push = function() {
    for( var i = 0, l = arguments.length; i < l; i++ ) this[this.length] = arguments[i];
    return this.length;
};
Array.prototype.push=function(){
对于(var i=0,l=arguments.length;i

您可以在前面添加一些内容。

我想在对象被推送到数组后调用函数,因此我执行了以下操作:

myArray.push = function() { 
    Array.prototype.push.apply(this, arguments);
    myFunction();
    return myArray.length;
};

function myFunction() {
    for (var i = 0; i < myArray.length; i++) {
        //doSomething;
    }
}
myArray.push=function(){
Array.prototype.push.apply(这是参数);
myFunction();
返回myArray.length;
};
函数myFunction(){
对于(var i=0;i
首先需要子类
数组

ES6():

es5:(proto几乎不受欢迎,但它是目前唯一的解决方案)


这个问题由来已久,但对于那些现在会发现这个问题的人来说,有另一种更为自然的方法来实现这一点:


@pottedmeat:但它的可读性较差——记住,程序不仅是为机器编写的,也是为其他程序员编写的@有些人非常感谢您对原型的清晰解释,而不是!当分配给
Array.prototype.push
时,您必须非常小心
//在这里做您想做的
代码。如果其中的任何内容导致调用
Array.push()
(甚至是间接的),您将导致JavaScript崩溃,并导致堆栈溢出。我只是想通过指出忽略它会很容易导致严重的不愉快来强化您的建议消息。太好了!那么基于索引的赋值呢?为什么没有集合(索引)或其他方法可以引发事件?Array.prototype.push.call(this,data);无法使用,因为阵列可以容纳多个项目。Array.prototype.push.apply(这是参数);好奇为什么有人不这么做?JS已经改变了。
class SortedArray extends Array {
    constructor(...args) {
        super(...args);
    }
    push() {
        return super.push(arguments);
    }
}
function SortedArray() {
    var arr = [];
    arr.push.apply(arr, arguments);
    arr.__proto__ = SortedArray.prototype;
    return arr;
}
SortedArray.prototype = Object.create(Array.prototype);

SortedArray.prototype.push = function() {
    this.arr.push(arguments);
};
const target = [];

const handler = {
  set: function(array, index, value) {
    // call callback function here

    // The default behavior to store the value
    array[index] = value;

    // Indicate success
    return true;
  }
};

const proxyArray = new Proxy(target, handler);