I was told something about js module framework two years ago. But without a deep dive into it, so that I have to ride out large bundles of js files in a mess on our projects for a long time. After kejun’s YUI presentation on D2 2009, I thought that it’s time to change.
Why use js module?
1, better js organization, readable + flexible + dependencies ordering
2, better performance, asynchronize loading JIT
How we did it before?
1, inline script in jsp : hard to unittest, make the file looks tedious and ugly
2, separated js script file: bad organization, too many ugly import tag <script>
What is the existed solution?
1, YUI module: http://developer.yahoo.com/yui/yuiloader/
2, Jawr: a tunable packaging solution for Javascript and CSS written in java
3, jQuery getSscript: http://ejohn.org/blog/degrading-script-tags/
4, jspkg: http://jspkg.sourceforge.net/docs/index.html
5, module.js http://ajaxian.com/archives/modulesjs-a-new-stand-alone-javascript-module-loader
$('selector').data('meaningfullname', 'this is the data I am storing');
// then later getting the data with
$('selector').data('meaningfullname');
$('div.edit').livequery('click', function(){
//go into edit mode
});
var hashCode = function(element){
return element.sort().toSource();
}
Array.prototype.dell = function(hashCode){
var deleList = [];
var obj = {};
do {
var ele = this.pop();
var key = hashCode(ele);
if (obj[key]) {
deleList.push(ele);
}
else {
obj[key] = ele;
}
}
while (this.length > 0);
for (var key in obj) {
this.push(obj[key]);
}
return deleList;
}
var list = [[3, 1], [1, 2], [1, 3]]
expect([[1, 3]]).to(equal, list.dell(hashCode));
expect([[1, 2], [1, 3]].sort()).to(equal, list.sort());
for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
if( oNode.nodeValue.match(/^\s*extra.*free/g) ) {
//this creates the expression
//it will be cached and re-used next time through the loop
}
}
for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
if( oNode.nodeValue.match(new RegExp(“^\s*extra.*free”,”g”)) ) {
//this will always create a new copy of the expression,
//and is generally much slower than creating static expressions.
}
}
var _table =$("#tableId")
for (var index in json) {
otherFun(_table, json[index]);
};
var logger=window.console && window.console.dir
var logger=window.console || {}
function f2c(s) {
var test = /(\d+(\.\d*)?)F\b/g; //Initialize pattern.
return(s.replace
(test,
function($0,$1,$2) {
return((($1-32) * 5/9) + "C");
}
)
);
}