0

I have array and I want to customize some of his prototype's methods. If i use:

arr.push = function(){
  doSomething()
  Array.prototype.push.call(arguments);
}

I'm creating own property push for arr. But I want to make new array-like object that will have prototype with method push from last example. I've tried to make such thing:

// creating new object which prototype linked to _Array.prototype
var newArr = new (function _Array(){});

// setting up method     
_Array.prototype.push = function(){...}

// copying all data
arr.forEach(function(val, index){ 
  newArr[index] = val;
});
newArr.length = arr.length

//updating original array
arr = Array.prototype.slice.call(newArr);

Yes, after that I will got array-like object, but Array.prototype.slice returns object binded with Array prototype not created by me _Array.prototype.

So can I create array with custom prototype?

2 Answers 2

1

You can create your own array this way:

// create own Array object
var _Array = function()  {};

// inherit from Array
_Array.prototype = Object.create(Array.prototype);
// reset constructor
_Array.prototype.constructor = _Array;
// overwrite push method
_Array.prototype.push = function(){
    doSomething()
    Array.prototype.push.apply(this, arguments);
};

You can use it like this:

// create own array
var newArr = new _Array();

// push to array
newArr.push(1, 2, 3);

// create copy
var arr = newArr.slice();
Sign up to request clarification or add additional context in comments.

2 Comments

After execution arr is empty. And when I do arr.push() - calls Array.prototype.push, not _Array.prototype.push. Purpose in ability to create arrays with custom prototype whose methods do something stuff and after it call original method of Array.prototype
I don't think you can subclass Array: perfectionkills.com/…
1

I don't think you can subclass Array: http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/

For example; using _Array:

var arr = new _Array();
arr[10]=22;
console.log(arr.length);//=0

Depending on how much you would like your extended array to behave like an array.

It may be better to just add the function on every array instance in this case and leave the array instance as an array instance instead of creating a type that "inherits" from Array (as it's not possible to inherit from Array):

function createSpecialArray(){
  var ret = new Array(arguments);
  //shadow push
  ret.push = createSpecialArray.push;
  return ret;
};
createSpecialArray.push=function(){
  //do other stuff
  console.log('doing other stuff');
  Array.prototype.push.call(this,arguments);
}

var arr = createSpecialArray(1,2,3);
console.log(arr);
arr[10]=22;
console.log(arr.length);
arr.push(33);
console.log(arr.length);

2 Comments

Thanks for reference. Final purpose was ability to create objects exactly replicating Array behavior in cases of methods that I not changed (up to truthy result for Array.isArray), but, now I see that JS can't do it.
On this evidence, I think own properties is the only option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.