Javascript - Performing actions on Array elements on creation [duplicate]

你。 提交于 2020-01-07 03:14:06

问题


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.


回答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.



来源:https://stackoverflow.com/questions/36067412/javascript-performing-actions-on-array-elements-on-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!