Overriding Array Push Method

by andypotanin

JavaScript

var list = [ 'one' ]; // Create new array 

Object.defineProperty( list, 'push', {
  value: function push() {
    console.log( "Pushing!", arguments ); // Perform any custom logic
    return Array.prototype.push.apply( this, arguments ); // Push the data using the native  method
  },
  enumerable: false,
  writable: true
});

list.push( 'two', 'three' ); // Push values; our custom push method will be used now.

console.log( 'list:', list ); // -> list: ["one", "two", "three"]