Is it possible to subclass and inherit from javascript Arrays?
I'd like to have my own custom Array object that has all the features of an Array, but contains additional properties. I'd use myobj instanceof CustomArray
to perform specific operations if the instance is my CustomArray.
After attempting to subclass and running into some problems, I found this Dean Edwards article that indicates doing this with Array objects doesn't work right. It turns out Internet Explorer doesn't handle it properly. But I'm finding other issues as well (only tested in Chrome so far).
Here's some sample code:
/** * Inherit the prototype methods from one constructor into another * Borrowed from Google Closure Library */ function inherits(childCtor, parentCtor) { function tempCtor() {}; tempCtor.prototype = parentCtor.prototype; childCtor.superClass_ = parentCtor.prototype; childCtor.prototype = new tempCtor(); childCtor.prototype.constructor = childCtor; }, // Custom class that extends Array class function CustomArray() { Array.apply(this, arguments); } inherits(CustomArray,Array); array = new Array(1,2,3); custom = new CustomArray(1,2,3);
Entering the following in Chrome's console gives this output:
> custom [] > array [1, 2, 3] > custom.toString() TypeError: Array.prototype.toString is not generic > array.toString() "1,2,3" > custom.slice(1) [] > array.slice(1) [2, 3] > custom.push(1) 1 > custom.toString() TypeError: Array.prototype.toString is not generic > custom [1]
Obviously, the objects don't behave the same. Should I give up on this approach, or is there some way to accomplish my goal of myobj instanceof CustomArray
?
In JavaScript, Array is a built-in global object that allows you to store multiple elements at once. In this reference page, you will find all the Array methods and their properties. For example, the sort() method of an Array is used to sort all the elements of that array using default or custom sorting rules.
In JavaScript, arrays are created with square brackets [...], and elements are indexed. An array-like is an object. Has indexed access to the elements and a non-negative length property to know the number of elements in it. These are the only similarities it has with an array.
Creating an Array Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.
prototype allows you to add new properties and methods to arrays. prototype is a property available with all JavaScript objects.
Juriy Zaytsev (@kangax) just today released a really good article on the subject.
He explores various alternatives like the Dean Edwards iframe borrowing technique, direct object extension, prototype extension and the usage of ECMAScript 5 accessor properties.
At the end there is no perfect implementation, each one has its own benefits and drawbacks.
Definitely a really good read:
class SubArray extends Array { last() { return this[this.length - 1]; } } var sub = new SubArray(1, 2, 3); sub // [1, 2, 3] sub instanceof SubArray; // true sub instanceof Array; // true
Original Answer: (Not recommended, may cause performance issues)
Copy-pasting from article mentioned in the accepted answer for more visibility
__proto__
function SubArray() { var arr = [ ]; arr.push.apply(arr, arguments); arr.__proto__ = SubArray.prototype; return arr; } SubArray.prototype = new Array;
Now you can add your methods to SubArray
SubArray.prototype.last = function() { return this[this.length - 1]; };
Initialize like normal Arrays
var sub = new SubArray(1, 2, 3);
Behaves like normal Arrays
sub instanceof SubArray; // true sub instanceof Array; // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With