Javascript - 对创建的数组元素执行操作 [英] Javascript - Performing actions on Array elements on creation
问题描述
我试图在Javascript中创建一个对象/类,它的行为像一个数组,但增加了一些功能。
I'm trying to create an Object/Class in Javascript that behaves like an Array but with some added functionalities.
我使用这些简单的线:
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')
自动获取变量 a
/ p>
And automatically, get variable a
to be (for example):
console.log(a)
["HELLO", "GOODBYE", "GOOD AFTERNOON"]
我知道如何使用循环和数组函数知道如果有无法覆盖构造函数(在此 Newclass
),因此它会在创建时自动应用于其所有元素,
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.
感谢大家的时间和答案。但是,我必须说这不是重复,因为我不是问如何使用参数
(或如果它们存在),但如何使用它们在构造一个数组派生类,我发现是完全不同的。
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.
即使知道参数
参数存在,我仍然不知道如何处理这些参数的构造函数 Array
,并且仍然具有此类对象的所有本机函数。
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.
推荐答案
您可以制作自己的数组派生:
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(', ') + ']');
您可以修改push方法以获取无限量的参数。但请注意,这是不一个完整的数组, a [5] ='thingy'
将不修改数组的长度,所以一定要使用只有方法来添加和删除你的数组。
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.
这也标识为 Array
和 uppercaseStringArray
使用 instanceof
。您可以向数组中添加自己的方法,例如您的 get_by_id
函数。
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.
这篇关于Javascript - 对创建的数组元素执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!