JavaScript模塊化的實現方式:
<!DOCTYPE HTML>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>模塊化</title></head> <body><script type="text/javascript" charset="utf-8">//方式一var moduleA;moduleA = function(){var prop = 1;function func(){};return{func:func,prop:prop}}();//方式二var moduleB;moduleB = new function(){var prop = 1;function func(){};this.prop = prop;this.func = func;};
</script> </body>
</html>
好處:便于分類管理使結構清晰同時也起到了規避變量名沖突。