0

I am trying to extend the Array object in Javascript exactly like this. I am trying to manipulate the values of the array (present in the this object) using the prototype like in the function.

function clear() {
    this = [];
}
Object.defineProperty(Array.prototype, 'clear', { value: clear, enumerable: true, });

It is giving me a error Uncaught SyntaxError: Invalid left-hand side in assignment

I am aware that the this is immutable. What is the workaround? I am not looking to create a new Object that emulates the Array constructor.

Gary
  • 2,293
  • 2
  • 25
  • 47

1 Answers1

1

One option would be

this.splice(0, this.length)

I guess you can also do this.length=0 directly, but it feels kinda hacky.

gog
  • 10,367
  • 2
  • 24
  • 38