Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to extend Array object in javascript

i try to extend Array object in javascript with some user friendly methods like Array.Add() instead Array.push() etc...

i implement 3 ways to do this. unfortunetly the 3rd way is not working and i want to ask why? and how to do it work.

//------------- 1st way Array.prototype.Add=function(element){      this.push(element); };  var list1 = new Array(); list1.Add("Hello world"); alert(list1[0]);  //------------- 2nd way function Array2 () {     //some other properties and methods };  Array2.prototype = new Array; Array2.prototype.Add = function(element){   this.push(element);   };  var list2 = new Array2; list2.Add(123); alert(list2[0]);  //------------- 3rd way function Array3 () {     this.prototype = new Array;     this.Add = function(element){       this.push(element);       }; };  var list3 = new Array3; list3.Add(456);  //push is not a function alert(list3[0]); // undefined 

in 3rd way i want to extend the Array object internally Array3 class. How to do this so not to get "push is not a function" and "undefined"?

Here i add a 4th way.

//------------- 4th way function Array4 () {     //some other properties and methods     this.Add = function(element){         this.push(element);     };  }; Array4.prototype = new Array();  var list4 = new Array4(); list4.Add(789); alert(list4[0]); 

Here again i have to use prototype. I hoped to avoid to use extra lines outside class constructor as Array4.prototype. I wanted to have a compact defined class with all pieces in one place. But i think i cant do it otherwise.

like image 765
demosthenes Avatar asked Jul 05 '12 04:07

demosthenes


People also ask

Can you extend array in JavaScript?

You CANNOT extend the Array Object in JavaScript. Instead, what you can do is define an object that will contain a list of functions that perform on the Array, and inject these functions into that Array instance and return this new Array instance. What you shouldn't do is changing the Array.

Does array extend object?

As we already know, native classes extend each other. For instance, Array extends Object . Normally, when one class extends another, both static and non-static methods are inherited.

What is extended array?

An array in python is used to store multiple values or elements of the same datatype in a single variable. The extend() function is simply used to attach an item from iterable to the end of the array. In a simpler term, this method is used to add an array of values to the end of a given or existing array.


1 Answers

ES6

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 

Using __proto__

(old answer, not recommended, may cause performance issues)

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 
like image 123
laggingreflex Avatar answered Sep 30 '22 15:09

laggingreflex