I have a javascript framework that adds an API property to elements. It supplies an interface through this property.
<script type="javascript">
var $slider= document.getElementById('mySlider');
//this function inits a slider and adds API property o element
initMyFancySlider($slider);
//now I can access API
$slider.API.next();
$slider.API.play();
// etc...
<script>
This is not accomplished by extending any DOM prototype. The API property is directly linked on Element
<script type="javascript">
function initMyFancySlider(element){
//some staff
Object.defineProperty(element, 'API', {
enumerable: false,
configurable: false,
value: MyFancySliderManagerInstance.API;
});
}
<script>
Some say adding properties to DOM elements is not a good practice. But I find it very useful for CMS type projects. Users can easily access API to customize an interface.
What is your point of view? Is there a better approach you can provide?
Just to make sure, in fact DOM element is not storing data, it just proxies to some API functionality. I know DOM elements are not for storing data or functionality. For instance $element.API.play()
is just a method like $element.blur()
. They are not related with data stored on element.
I don't see any performance overhead issue here. The only risk I can see here is element.API
property may be used by DOM specification in the future. But i think it is a very low probability because no DOM properties are specified in uppercase. They always exist in camelCase (until now).
Thanks in advance.