javascriptdefineproperty

Object.definePropery on Array.prototype.push doesnt work


I want to unable the overriding option on Array.prototype.push on my web application.

I have tried to do so with Object.defineProperties(Array.prototype, "push", {writable: false});

But got an Uncaught TypeError: Property description must be an object: p

What did I do wrong?

Thanks


Solution

  • It's really not a good idea to do that with the prototype of a native object, this have some strong performance implications as described here

    Now that you've been warned, if you want to go ahead, just use:

    Object.defineProperty(Array.prototype, "push", {writable: false});
    

    A better solution without the performance cost is to set writable to false specifically to the array object(s) you want to "protect".