收集各种实现中!http://blog.nyarla.net/2006/11/28/1
var package = function ( className, CodeBlock ) { var classes = className.split('.'), class = new String, obj; for ( var i = 0, len = classes.length; i < len; i++ ) { if ( i > 0 ) { class += '.' } class += classes[i]; var evalCode = "if(typeof(" + class + ")=='undefined'){" + class + "={}}"; ( i == len - 1 ) ? obj = eval( evalCode ) : eval( evalCode ) ; } if ( typeof(CodeBlock) == 'function' ) { CodeBlock.call(obj); } return obj; }
package('foo'); package('foo.bar', function () { var self = this; self.say = function () { return alert('Javascript package!!'); } });