Add properties to element [Node]

3

Is there a problem in adding custom properties directly to the [Node] element? For example:

document.getElementById('minhaDiv').minhaPropriedade = 'teste';

I'm using Firefox and have had no problems setting up or getting ownership, but is it something I should avoid? Why?

    
asked by anonymous 23.01.2015 / 17:48

1 answer

5

It is something that is generally recommended to avoid. Primary reason: You could end up causing a name collision with a property that might be implemented in the DOM in the future. So, even if the code does not cause any problems today, it may come up in the future.

Another thing is that this is half way to later on you feel like extending the DOM, that is, extending the prototypes of native DOM objects as HTMLElement . This is not a good idea as these objects are much more susceptible to peculiarities that vary by implementation - and it is the very specification of the language that gives this freedom to implementors of so called host objects .

For more details on all of this, the great reference is the following Kangax article: Although the article is from 2010, when the browser landscape was different, the language specification has not changed (yet), so much of what is said there is still valid - except, perhaps, for specific performance and bug analyzes of particular browsers.

    
23.01.2015 / 18:00