0

I'm trying to create an Object/Class in Javascript that behaves like an Array but with some added functionalities.

I've achieved this with these simple lines:

var Newclass = Array
Newclass.prototype.get_by_id = function(){}

However, I'm trying to perform some actions just when I call this new class, so elements I'm adding to this are treated (and transformed, if needed) in a specific way.

I'm wondering if there is a way of making it on the fly, so I could do something like:

var a = New Newclass('hello', 'goodbye', 'good afternoon')

And automatically, get variable a to be (for example):

console.log(a)
["HELLO", "GOODBYE", "GOOD AFTERNOON"]

I know how to do it with loops and Array functions (like map and so), but I'd like to know if there is anyway to overwrite the constructor (on this Newclass) so it gets applied automatically for everyone of its elements on creation, without breaking anything.

EDIT

Thank you everyone for your time and answers. However, I must say this is not a duplicate, as I'm not asking how to work with arguments (or if they exist), but how to work with them on the construction of an Array derivated class, which I find is totally different.

Even knowing the arguments parameter exists, I still don't know how to process these arguments on the constructor of the Array and having still all the native functions of this kind of object.

10
  • But use carefully the arguments object, because is in proccess of removing from standards Commented Mar 17, 2016 at 17:24
  • So what you are telling me is to overwrite the constructor method, taking each one of the arguments and making a a.push(argument_transformed) to itself? Commented Mar 17, 2016 at 17:25
  • You don't need push since arguments is an array that contains all of the arguments passed, no matter how many. I don't think you need to override the constructor, maybe you can use call() or bind() to add the correct scope. Sorry for the little help, I can't imagine right now another way to make this task Commented Mar 17, 2016 at 17:27
  • 1
    var Newclass = Array You haven't actually created a new class. Commented Mar 17, 2016 at 17:39
  • I have created the base for a new class. After this, I can make methods that apply only to Newclass in stead of setting prototype for all arrays. Commented Mar 17, 2016 at 17:41

1 Answer 1

1

You can make your own derivative of an Array:

function uppercaseStringArray(){
    Array.call(this);
    for(var i = 0; i < arguments.length; i++) this.push(arguments[i]);
}
uppercaseStringArray.prototype = Object.create(Array.prototype);
uppercaseStringArray.prototype.push = function(string){
    Array.prototype.push.call(this, string.toUpperCase());
}

This works exactly like you expect and it still has all the properties normal arrays have:

function uppercaseStringArray(){
    Array.call(this);
    for(var i = 0; i < arguments.length; i++) this.push(arguments[i]);
}
uppercaseStringArray.prototype = Object.create(Array.prototype);
uppercaseStringArray.prototype.push = function(string){
    Array.prototype.push.call(this, string.toUpperCase());
}

var a  = new uppercaseStringArray('tomato', 'apple', 'pear');

console.log(a);

document.write('[' + a.join(', ') + ']');

You could modify the push method to take an unlimited amount of arguments. Please note, however, that this is not a full array, as a[5] = 'thingy' will not modify the length of your array, so be sure to use only methods to add and remove from your array.

This also indentifies itself as both an Array and an uppercaseStringArray using instanceof. And you can add your own methods to the array, like your get_by_id function in its prototype.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer right now, so I mark it as solved! Thank you for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.