WY-产品前端架构

2015-09-15  本文已影响214人  ddai_Q

模块


例子:

//math.js
var math = {
   add : function add( a , b ){ return  a+b },
   sub : function mul( a , b ){ return  a-b}
}

//caculator.js
var caculator = ( function( m ){
     var action = 'add'
     function compute( a , b ){
         switch( action ){
            case 'add' : return m.add(a,b);
            case 'sub' : return m.mul(a,b);
         }
     } 
     return{ 
        compute : compute
    }
})( math )

该 caculator 的问题:

命名空间


math.js

namespace('math',[],funciton){
    function add(a,b){ return a+b;}
    function sub(a,b){ return a-b;}    
    return{
        add : add,
        sub : sub
    } 
})

caculator.js

namespace('caculator',["math"],function( m ){
    var action = "add";
    function compute( a,b ){
        return m[action]( a,b );
    }
    return {
        compute : compute
    }
});

备注: 有依赖申明 ["math"], 依赖的math被传入( m )

模块系统


1、依赖管理 (记载、分析、注入、初始化)

2、决定模块写法

commonjs/module
A module spec for javascript outside the browser

math.js

 function add( a , b ){ return  a+b },
 function sub( a , b ){ return  a-b}
 exports.add = add
 exports.sub = sub
commonjs

优点

缺点

AMD Asynchronous Module Definition

math.js

  define( [] ,function(){
       function add( a , b ){ return  a+b },
       function sub( a , b ){ return  a-b}

       //接口暴露
       return{
          add : add,
          sub : sub      
      }
  } )

caculator.js

依赖注入

caculator.js

loader plugins 草案
可以加载除了js以外的别的文件

loader plugins

AMD优点

AMD缺点

ES6/module


es6示例

ES6优点

ES6缺点

Systemjs

Universal dynamic module loader 统一动态module loader


支持加载AMD
支持加载 Commonjs
支持加载ES6
支持transpiler, 可支持任意资源

框架


库与框架

框架

框架

JS框架


** 1、mootoos 最好的源码阅读学习的资源,建议小型项目中使用 **

mootoo

优点

缺点

**2、jquery 最稳妥的方案 **

jquery

优点

缺点

**3、zepto 移动端的备选品 **


zepto

优点

缺点

4、hammer.js 手势相关

hammer

5、iscroll.js 局部滚动

iscroll

**6、velocity.js 高级动画 **


velocity

**7、video.js 视频播放 **


video.js

选框架的时候,Issue的总量与解决率比start 更重要

Communication 通信

框架比较


boostrap&foundation

boostrap&foundation

Routing 路由


routing 库

routing库

架构


目的:Architecture 解耦 MVC/MVVM/MV*

工程构建

template webapp
上一篇 下一篇

猜你喜欢

热点阅读