1

I was trying to make a method inside an array object to delete an element from my array object. Here is the code:

a.__proto__.remove = index => { this.splice(index,1) };

But I am getting an unexpected error when I am trying to execute this method like below:

TypeError: this.splice is not a function

But why? Please help me to get out of this error. Here is my full code:

a = [1,2,3,4,5];
a.__proto__.remove = index => { this.splice(index,1) };
a.remove(3);

Thanks a ton.

  • 1
    Use `function (index) { }` instead: https://jsfiddle.net/khrismuc/5n2p37kv/ (also: `Array.prototype`, and `1`) – Chris G Feb 04 '19 at 08:06
  • 1
    I agree with @MoSwilam. This has everything we want in a question — it's written clearly, demonstrates some effort, and has a minimal complete example. It may be a dupe, but that dupe is quite difficult to find if you are struggling with the concept of `this` in javascript, which we all know is not obvious or intuitive when you're starting out. – Mark Feb 04 '19 at 08:21

0 Answers0