Nahush Farkande Nahush Farkande - 15 days ago 6
Javascript Question

Augmenting Object in React

I am working on a project where I have to attach a few utility functions to Javascript

Object
object as follows:

function isEmpty(a, b) {...}
Object.prototype.isEmpty = isEmpty;


Now the problem i am facing is, since I am working with react, I am guessing the above code is also attaching the
isEmpty
function to the constructed Components. And this works fine as long as I don't use the native html tags i.e.
div
,
span
inside my components which is not possible. I get the following warning

Warning: Unknown prop `isEmpty` on <div> tag.
Remove these props from the element. For details, see
https://facebook.github.io/react/warnings/unknown-prop.html


when I use the native html tags. Is there any way to augment the
Object
object without getting this error in react?

Answer

The problem is that an object extension like this is enumerable. You need to use defineProperty

BTW: this is still a bad idea