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=

随便看看

ipadmini从9.3.5降级8.4.1并完美越狱

ipadmini之前是iOS9.3.5实在是卡的用不了,于是打算降级,但是尝试了包括改版本描述等很多方法一直失败。今天突然成功降级8.4.1并且完美越狱,运行流畅了非常多。方法如下:打开网址:https://www.i4.cn/news_detail_18447.html,下载对应设备的8.4.1自制固件,一般的固件是不可以的。...

mysql之排序查询

高级文章目录3:排序查询功能:1.按单个字段排序案例1:查询员工信息,要求工资从高到低排序2.为排序添加筛选条件案例1:部门编号˃=90的员工信息,按员工编号降序排序案例2:部门编号˃=90的人员信息,按输入时间排序。按表达式排序案例1:按年薪显示员工信息和年薪4按别名排序案例1按年薪升序查询员工信息5.按函数(长度)排序案例1查询员工姓名并按姓名长度减少...

LaTex学习笔记(1)——LaTeX文档插入图片的几种常用方法

2,插入bmp图片还没有找到直接插入bmp图片的方法。用gimp或fastoneimageviewer,将jpg质量选为最高,转换之后得到的图片质量较好。3,同时插入jpg和eps图片插入的命令不变。编译时使用Latex,dvi2pdf,两种格式的图片都可以显示。...

WPF绑定功能常用属性介绍

这是实质上是System.Windows.Data.BindingMode.OneWay绑定的一种简化形式,它在源值不更改的情况下提供更好的性能。确定依赖属性绑定在默认情况下是单向还是双向的编程方法是:使用System.Windows.DependencyProperty.GetMetadata获取属性的属性元数据,然后检查System.Windows.Fr...

如何从github下载项目的源代码,包含git客户端,直接下载,vs下载

很多小伙伴可能刚刚联系了github。如果他们使用github下载项目,他们将在这里编写一个统一的声明。从各种方式下载源代码,以加深对git的理解。英文描述:Git是一个免费开源的分布式版本控制系统,旨在以快速和高效的方式处理从小型到大型项目的所有事务。例如:在github上下载项目:https://github.com/dathlin/HslCommuni...

文件(夹)对比利器WinMerge

IDE中自带的svn功能较弱,还好有winMerge弥补了它的缺陷,它可以对比文件、文件夹,使用起来还是较为方便,界面也是中文。“开始”菜单,弹出对话框中选择需要进行对比的文件夹或文件然后选择一个过滤器,它自带就可以过滤掉svn目录,如需要过滤其它一些指定的目录,则需要自己修改过滤器的规则了,也很简单。...