I needed this small function for one of my project.
function fetchAttrs(node) {
if (!node || !node.hasAttributes()) return {};
var attrs = node.attributes;
var res = {};
var length = attrs.length;
for (var i = 0; i < length; i++) {
var attr = attrs[i];
res[attr.name] = attr.value;
}
return res;
}
It's working fine as expected but I wanted to know if I am missing something from a performance point of view or good coding practices. Just need a second opinion :)
Update
How to handle exceptions, is it a good practice?
node
has both.hasAttributes()
and.attributes
or it will throw an exception. \$\endgroup\$