javascript设计模式(2)

技巧型设计模式

27.链模式 return this;

28.委托模式

点击事件委托到父元素上,内存外泄,数据分发

29.数据访问对象模式

var BaseLocalStorage = function(preId,timeSign){

this.preId = preId;

this.timeSign = timeSign || '|-|';

}

BaseLocalStorage.prototype = {

status:{

SUCCESS:0,

FAILURE:1,

OVERFLOW:2,

TIMEOUT:3

},

storage:localStorage || window.localStorage,

getKey:function(key){

return this.preId +key;

},

set:fucntion(key,value,callback,time){

var status = this.status.SUCCESS,

key = thsi.getKey(key);

try{

time = new Date(time).getTime()||time.getTime();

}cache(e){

time = new Date().getTime()+1000*60*60*24*31;

}

try{

this.storage.setTime(key,time+this.timeSign+value);

}catch(e){

status = this.status.OVERFLOW;

}

callback && callback.call(this.status,key,value);

},

get:fucntion(){

var status = this.status.SUCCESS,

key = this.getKey(key),

value = null,

timeSignLen = this.timSign.length,

that=this,

index,

time,

result;

try{

value = that.storage.getItem(key);

}catch(e){

result = {

status:that.status.FAILURE,

value:null

};

callback && callback.call(this,result.status,result.value);

return result;

}

if(value){

index = value.indexOf(that.timeSign);

time =+value.slice(0.index);

if(new Date(time).getTime > new Date().getTime() || time == 0){

value = value.slice(index+timeSignLen);

}else{

value = null,

status = that.status.TIMEOUT;

that.remove(key);

}

}else{

status = that.status.FAILURE;

}

result = {

status:status,

value:value

};

callback && callback.call(this,result.stauts,result.value);

return result;

},

remove:function(){}

var status = this.status.FAILURE,

key = this.getKey(key),

value=null;

try{

value = this.storage.getItem(key);

}cache(e){

if(value){

try{

this.storage.removeItem(key);

status=this.status.SUCCESS;

}catch(e){}

}

callback && callback.call(this,status,status>0?null:value.slice(value.indexOf(this.timeSign)+this.timeSign.length))

}

}

node.js MongoDB

30.节流模式

var throttle = function(){

var isClear = arguments[0],fn;

if(type of isClear === 'boolean'){

fn = arguments[1];

fn._throttleID && clearTimeout(fn._throttleID);

}else{

fn = isClear;

param = arguments[1];

var p = extend({

context:null,

args:[],

time:300

},param);

arguments.call(true,fn);

fn._throttleID = setTimeout(function(){

fn.apply(p.context,p.args)

},p.time)

}

}

31.简单模板模式

var A= A ||{};

A.root = document.getElementById('container');

A.strategy = {

'listPart':function(){}

}

A.init = function(data){

this.strategy[data.type](data);

}

//模板渲染方法

A.formateString = function(str,data){

return str.replace(/\{#(\w+)#\}/g,function(match,key)){

return typeof data[key] === undefined ?'':data[key];

}

}

//模板生成器

32.惰性模式

//加载即执行

var A = {};

A.on = function(dom,type,fn){

if(document.addEventListener){

return function(dom,type,fn){

dom.addEventListener(type,fn ,false);

}

}

}();

//惰性执行

A.on=function(dom,type,fn){

if(dom.addEventListener){

A.on =function(dom,type,fn){

dom.addEventListener(type,fn,false);

}

}

A.on();

}

33.参与者模式

//函数绑定

function bind(fn,context){

return function(){

return fn.apply(context,arguments);

}

}

//函数柯里化

function curry(fn){

var Slice = [].slice;

var args = Slice.call(arguments,1);

return function(){

var addArgs = Slice.call(arguments),

allArgs = args.concat(addArgs);

return fn.apply(null,allArgs);

}

}

//函数柯里化后的函数绑定

function bind(fn,context){

var Slice = Array.prototype.slice,

args = Slice.call(argument,2);

return fucntion(){

var addArgs = Slice.call(arguments),

allArgs = addArgs.concat(args);

return fn.apply(context,allArgs);

}

}

34.等待者模式

var Waiter = function(){

var dfd =[],

doneArr=[],

failArr=[],

slice = Array.prototype.slice,

that=this;

var Primise = fucntion(){

this.resolved = false;

this.rejected = false;

}

Primise.prototype = {

resolved:function(){

this.resolved = true;

if(!dfd.length){

return;

}

for(var i = dfd.length-1;i>=0;i--){

if(dfd[i] && !dfd[i].resolved || dfd[i].rejected){

return;

}

dfd.splice(i,1)

}

_exec(doneArr);

},

reject:function(){

this.rejected = true;

if(!dfd.length){

return;

}

dfd.splice(0);

_exec(failArr);

}

}

that.Deferred = function(){

return new Primise();

}

function _exec(arr){

var i = 0,

len = arr.length;

for(;i

try{

arr[i]&&arr[i]();

}catch(e){}

}

}

this.when = function(){

dfd = slice.call(argument);

var i = dfd.length;

for(--i;i>=0;i--){

if(!dfd[i] || dfd[i].resolved || dfd[i].rejected || !dfd[i] instanceof Peimise){

dfd.splice(i,1);

}

}

return that;

};

that.done = function(){

doneArr = done Arr.concat(slice.call(arguments));

return that;

};

that.fail = function(){}

failArr = failArr.concat(slice.call(arguments));

return that;

}


架构型设计模式

35.同步模块模式

模块定义,模块调用

36.异步模块模式

模块定义,模块调用

37.Widget模式

模板引擎:处理数据,获取模板,处理模板,编译执行

38.MVC模式

39.MVP模式

模型model,视图view,管理器presenter

40.MVVM模式

双向绑定(data-binding):View的变动,自动反映在 ViewModel

模型model,视图view,视图模型viewmodel

你可能感兴趣的:(javascript设计模式(2))