0

I'm trying to create a custom function for objects in Next.js

I'm writting the following

Object.prototype._something = function (something) {
   console.log(something)
}

It works, but now it gives me a weird error

Warning: Invalid value for prop `_something` on <img> tag. Either remove it from the element, or pass a string or number value to keep it in the DOM.

But it is only with Object.prototype, if I declare the same function as Array.prototype everything workds fine

Saul Montilla
  • 687
  • 6
  • 16
  • I think the error arises from where you are using it. Are you using it with HTML tags anywhere? Please add tnhose details too – Tushar Shahi Sep 03 '21 at 19:23
  • Usually unwise to directly edit prototypes in JS. Why not a util function? – serraosays Sep 03 '21 at 19:28
  • 1
    Maybe have a look at [How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop](https://stackoverflow.com/q/13296340/1048572). But really, [don't put custom functions on `Object.prototype`](https://stackoverflow.com/q/14034180/1048572)! – Bergi Sep 03 '21 at 23:27

0 Answers0