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<len;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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 6,544评论 1 10
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 12,401评论 2 17
  • 三、闭包和高阶函数 3.1 闭包 3.1.1 变量的作用域 所谓变量的作用域,就是变量的有效范围。通过作用域的划分...
    梁同学de自言自语阅读 5,342评论 0 6
  • 生活工作在海岛,每天都在看海,特殊的工作让我对海有了不一样的感觉,现实中的海比你不像小说中描述的那样蔚蓝一片,不像...
    明天开始减肥阅读 1,270评论 0 0
  • 或许我的思维太不同于常人了些,喜欢上了T先生。当有人问我,你为什么会喜欢一个老男人时,我只能呵呵一笑,谁叫他正...
    Johnny05阅读 3,647评论 0 0

友情链接更多精彩内容