Helloworld,
I want to create a prototype of array.
Array.prototype.foo = function(){}
But my prototype must apply only if this array contain only a specific object like "bar" Is it possible to create a prototype like this in javascript? :
Array<bar>.prototype.foo = function(){}
Thank you! James
I think that you can do something similar. The best way that I can think to do is it to decorate the default JavaScript array with an additional function. Below is an example showing a print function working.
let test = ['a', 'b', 'c'];
function decoratedArray(args) {
let decorated = [...args];
decorated.print = () => {
decorated.forEach((arg) => {
console.log(arg);
});
}
return decorated;
}
test = decoratedArray(test);
test.print();