javascript culture
javascript culture
I know that every language has its own culture, the culture of JavaScript is definitely unique. I am a big fan of jQuery, but I have mixed feeling in the implementation of jQuery core. The passed in parameters get reused and overloaded for semantics in many places, is it part of javascript culture. I know this might help to improve performance, but I don't know the actual performance gain, but the cost of maintenance will definitely increase. I have changed dataAttr function in data module of jQuery core, and it is for my personal understanding. The code passed the unit test in firefox 3.6. Do you think it is better or worse? Thanks
function tryGetDataFromHtml5DataAttr(elem, key, data) {
//if data is has value already
//or elem is not element node
if (data !== undefined || elem.nodeType !== 1) {
return data;
}
// If nothing was found internally, try to fetch any
// data from the HTML5 data-* attribute
var attributeValue = elem.getAttribute("data-" + key);
if (typeof attributeValue !== "string") {
return undefined;
}
var setData;
try {
if (attributeValue === "true") {
setData = true;
} else if (attributeValue === "false") {
setData = false;
} else if (attributeValue === "null") {
setData = null;
} else if (!jQuery.isNaN(attributeValue)) {
setData = parseFloat(attributeValue);
} else if (rbrace.test(attributeValue)) {
setData = jQuery.parseJSON(attributeValue);
} else {
setData = attributeValue;
}
} catch(e) {
setData = attributeValue;
}
// Make sure we set the data so it isn't changed later
jQuery.data(elem, key, setData);
return setData;
}