If I do:
Array.prototype.test = "test"
Array.prototype.t = function() {return "hello"}
Every new Array
will have the property test
and the method t
.
How can I do the same without affecting all Arrays?
Like:
Names = function(arr){
// Contacts constructor must be the same of Array
// but add the property test and the function t
}
z=new Names(["john","andrew"])
So that z.test
will return "test"
and z.t()
will return "hello"
?
(but Array.test
and Array.t
would stay undefined
)
I explain better:
Array.prototype.t="test";
Array.prototype.test = function(){ return "hello";}
z=new Array("john", "andrew")
console.log(z);
But this affects ALL arrays. I want the same but with a new constructor Names that inherits Array constructor.
class Names extends Array {
constructor(...args) {
super(...args);
}
}
Names.prototype.t = 'test';
let z = new Names("john", "andrew")
z.push('Amanda')
console.log(z.t)
console.log(z)
You can easily set it at Names.prototype