1

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.

2
  • "// Contacts constructor must be the same of Array " this part is not clear. Do you mean Names instances should inherit Array.prototype as well? Commented Jul 2, 2018 at 7:09
  • I want a prototype that does not affect all arrays but only itself.. Array.prototype.test = "test" Array.prototype.t = function() {return "hello"} affects all arrays I wish to do Names.prototype.test = "test" Names.prototype.t = function() {return "hello"} where Names inherits everything else from Array prototype Commented Jul 2, 2018 at 8:44

4 Answers 4

2

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

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

5 Comments

not what I intended to do. I want the same effect of the Array.prototype example but affecting only Names and not all arrays.
@Zibri: Please have a look at my updated solution. z can access the push method just like Array and z can access t as you wanted. And it doesn't pollute Array, as you wanted
perfect! I was getting crazy!
the instructions I was searching for where the first lines class Names extends Array { constructor(...args) { super(...args); } } THANKS!
@Zibri: Glad to help! I know that feeling :D
1

Can't you just extend Array?

class Names extends Array {
  constructor(...args) {
    super(...args);
    this.t = "test";
  }

  test() { return "hello" }
}

let z = new Names("john", "andrew")

2 Comments

Nope.. executing your example returns: Names [t: "test"] instead of ["john","andrew"]
@Zibri Sorry, my bad, I actually forgot to pass the arguments to super
1

Here is a crude implementation:

function Names(arr) {
  this.contacts = enhanceArray(arr);
}

function enhanceArray(arr) {
  arr.test = 'helloProp';
  arr.t = function() {
    return 'helloFunc'
  }
  return arr;
}
let z = new Names(["john", "andrew"]);

console.log(z.contacts[0]);
console.log(z.contacts.test);
console.log(z.contacts.t());

Comments

0

You can create your own extended Array constructor factory, something like

(() => {
  const myArr = XArray();
  let a = myArr(["John", "Mary", "Michael"]);
  console.log(`myArr(["John", "Mary", "Michael"]).sayHi(1): ${a.sayHi(1)}`);
  console.log("demo: myArr(1, 2, 3) throws an error");
  let b = myArr(1, 2, 3); // throws
  
  // an extended array
  function XArray() {
    const Arr = function(arr) {
      if (arr.constructor !== Array) {
        throw new TypeError("Expected an array");
      }
      this.arr = arr;
    };
    Arr.prototype = {
      sayHi: function (i) { return `hi ${this.arr[i]}`; }
    };
    return arr => new Arr(arr);
  }
})();

4 Comments

could you check your code, it seems there is a problem when executing. ;)
@Ivan that's by design ;P
oh sorry, I didn't pay enough attention!
No worries, the answer was evidently not clear enough. Edited it a bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.