Javascript设计模式(摘译)

摘要:
2)structural--主要关注对象的组合方法Structuraldesignpatternsareonesthatfocusoneasingtherelationshipbetweendifferentcomponentsofanapplication.Theyhelptoprovidestabilitybyensuringthatifonepartoftheappchanges,theentirethingdoesn'tneedtoaswell.3)behavioral--主要关注对象间的通信方式Behavioraldesignpatternsemphasizeflexibilitythroughtheidentificationofcommoncommunicationpatternsbetweenvariousobjects.二、日常使用的javascript设计模式1)TheModulePattern2)TheRevealingModulePatternvaraccount=function(){varbalance=0;vardeposit=function{balance+=money;console.log;sendMsg();};varwithdraw=function{balance-=money;console.log;sendMsg();};//私有方法varsendMsg=function(){console.log("sendingmessage!

说明: 未完成。。。更新中。。。。

一、javascipt设计模式分类

设计模式分类有很多标准,最流行的三种如下

1) creational -- 主要关注对象创建

Creational design patterns deal directly with object initialization procedures focusing on the creation of situation-specific objects. Without thinking about how objects are created, a level of complexity can be added to the design of an application. Creational design patterns work to solve this problem by adding a layer to the object creation process.

创建型设计模式直接处理对象的初始化程序,重点关注创建基于特定场景的对象。它不关注如何创建对象,其复杂性的层次直接加入到应用程序的设计中。创建型设计模式通过在对象创建过程上加上一层来解决问题。

Javascript设计模式(摘译)第1张

2) structural-- 主要关注对象的组合方法

Structural design patterns are ones that focus on easing the relationship between different components of an application. They help to provide stability by ensuring that if one part of the app changes, the entire thing doesn't need to as well.

Javascript设计模式(摘译)第2张

3) behavioral-- 主要关注对象间的通信方式

Behavioral design patterns emphasize flexibility through the identification of common communication patterns between various objects.

Javascript设计模式(摘译)第3张

二、日常使用的javascript设计模式

1)The Module Pattern

2)The Revealing Module Pattern

    var account = function(){
        var balance = 0;
        var deposit = function(money){
            balance + =money;
            console.log("balance after deposti: ",balance);
            sendMsg();
        };
        var withdraw = function(money){
            balance -=money;
            console.log("balance after withdraw",balance);
            sendMsg();
        };
        //私有方法
        var sendMsg = function(){
            console.log("sending message!")
        };
        //公共方法 -- send outside module
        return{
            deposit:deposit,
            withdraw: withdraw
        }
    };
    var a1 =account();
    a1.deposit(100);
    a1.withdraw(20);
    a1.sendMsg();  //could have a alert

3)The Singleton Pattern

            var PersonSingleton =(function(){
                varinstantiated;
                functioninit(){
                    functionmyOtherMethod() {
                        alert( 'my other method');
                    }
                    return{
                        name: 'Anonymous',
                        work: function(){
                            console.log(this.name + "is working");
                        },
                        someOtherMethod: myOtherMethod
                    }
                }
                return{
                    //handles the prevention of additional instantiations
                    getInstance: function(){
                        if(!instantiated){
                            instantiated =init();
                        }
                        returninstantiated;
                    }
                }
            })();
            var p1 =PersonSingleton.getInstance(); 
            p1.work();  //return Anonymouse
            var p2 =PersonSingleton.getInstance();
            p2.work();  //return Anonymouse

4)The Observer Pattern

5)The Mediator Pattern

6)The Prototype Pattern

7)The Facade Pattern

8)The Factory Pattern

参看:

  • https://carldanley.com/javascript-design-patterns/#creational-design-patterns

免责声明:文章转载自《Javascript设计模式(摘译)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux环境下的GCC编译器与GDB调试工具介绍一些常用的颜色分类下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

RF(一)RF的安装步骤

7.安装Appium 8,安装最新版本的nodeJS:node-v6.9.49,在命令行上执行Appium命令,您应该能够启动Appium服务器~~~~...

git:将两个请求合并为一个请求

Gitrebase ihEAD~2解释:此命令可以以文本形式显示您提交的两次请求。如果数字2被4替换,则您最近四次提交的信息将显示如下:1 pick56a06efchange1:删除一个空白行2 pickedbeab5change2:addlogonMainActivity34#Rebase23198ba..Edbeab5onto23198ba5#6#命令:...

配置nginx

aNULL:!MD5:!...

"SQLserver 事务日志已满"解决方法

如果不够,备份后换个地方存[注:tempdb你数据库名称。...

C# winform开发嵌套Chrome内核浏览器(WebKit.net)开发(一)

//Www.cnblogs.com/Maxq/p/6566558.htmlWebKit.net是WebKit的一个net包。使用它,。net程序可以非常方便地集成和使用webkit作为加载网页的容器。EventArgse){WebKit.WebKitBrowser=newWebKitBrowser();this.Controls.Add(浏览器);...