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=

随便看看

怎样将shp文件的坐标点导出来?

单击以选择保存类型中的文本文件,将经度和纬度输出为txt格式。坐标系统有两个选项。第一个是数据源的坐标系。数据的数据源坐标系为UTM,投影坐标系,单位为米。第二个是我开始设置的数据帧的坐标系,即WGS84,单位为度。。。。直接将获得的点的坐标生成到文本文件中。如果它是栅格文件,则来自rastrastertopint的arctoolboxconverttool...

解决安卓手机连接wifi总弹出“已登录到Wlan网络”

一些Android机型在连接到wifi时会不断提示,“如果你不小心打开了这个提示,你会跳转到一些广告网站。这是因为谷歌服务器会在本地Android系统通电后发送连接请求。”。一些手机制造商的工程师将此连接服务器设置到一些广告网站并登录网络“我们可以管理设备或手机模拟器的状态,还可以执行许多手机操作,如安装软件、升级系统、运行shell命令等。adb是连接安卓...

Element UI 弹窗(Dialog)改成自适应高度,仅body内容部分滚动

定义样式如下:.abow_dialog{display:flex;justify-content:center;align-items:Center;overflow:hidden;.el-dialog{margin:0auto!important;height:90%;overflow:hidden;.el-dialog__body{position:ab...

CorelDRAW 编写和运行宏指令

在开发和运行CorelDRAW宏之前,必须安装VBA组件。在CoerlDRAW11和12中安装CorelDRAWVBAVBA是典型安装的一部分。2如果安装开始,请单击安装CorelDRAW12 Graphics Suite。CorelDRAWVBA工具栏CorelDRaw工具栏提供了几个快速的VBA函数和对VB编辑器的访问。但是,您可以通过在CorelDRA...

Oracle 12c新特性(For DBA)

2: Oracle12cIn-MemoryOracle12cIn-Memory提供了一种独特的双格式体系结构,它可以使用传统的行格式和新的内存列格式同时在内存中存储表。与其他NOSQL分片结构相比,OracleSharding提供了优异的运行时性能和更简单的生命周期管理。OracleSharding使用GDS体系结构自动部署和管理分片和复制技术。GDS还提供...

ArcGIS Server服务状态正在停止。。。问题BUG解决

1、 ArcGISServer服务器存在服务问题:ArcGISServerManager的所有服务都显示为“正在停止…”。N个解决方案1)港口占用问题。端口4000-4002已被其他程序占用,导致服务无法正常启动。您可以通过CMD找到相关PID占用的端口,停止它,然后启动GIS服务。您也可以在安装目录中将其手动更改为D:ProgramFiles ArcGIS...