How to extend Array.prototype.push()?

后端 未结 6 721
遥遥无期
遥遥无期 2020-11-27 02:45

I\'m trying to extend the Array.push method so that using push will trigger a callback method, then perform the normal array function.

I\'m not quite sure how to do

6条回答
  •  鱼传尺愫
    2020-11-27 03:22

    Array.prototype.push was introduced in JavaScript 1.2. It is really as simple as this:

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

    You could always add something in the front of that.

提交回复
热议问题