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=

随便看看

基于 WebRTC 的 RTSP 视频实时预览

该方案采用基于WebRTC的视频即时消息,其原生支持RTP协议的解码,因此延迟可以非常低,约为0.2-0.4秒。其他方案的延迟大于1秒。WebRTC需要浏览器。您可以在以下地址查看支持的浏览器。WebRTC实现基于web的视频会议。标准是WHATWG协议。其目的是通过浏览器提供简单的javascript来实现实时通信功能。Github中有很多WebRTC的实...

js 预览 excel,js-xlsx的使用

js-xlsx简介SheetJS生成的js-xls x是一个非常方便的工具库,只能使用纯js读取和导出excel。它功能强大,支持多种格式,支持xls、xlsx和ods等十几种格式。本文以xlsx格式为例。官方github:https://github.com/SheetJS/js-xlsx支持演示在线演示地址:http://demo.haoji.me/20...

C# 没落了吗?

首先,这个数字--------------------------------------------C#是否正在衰落与微软的整个平台密切相关。近年来,使用C#的人越来越少,这也是因为越来越少的人专门为Microsoft平台开发产品。现在是移动时代,微软基本上错过了互联网和移动互联网这两波浪潮。现在生活不容易。在软件工程中,人们常说“唯一不变的就是改变本身”...

go语言游戏服务端开发(一)——架构

本教程以Go语言为例。特别是游戏服务进程有更新上线时,稳定性还没有被线上并发验证,宕机的几率会增加,数据丢失的风险也会增加。为了减轻风险,可以考虑把数据缓存跟服务进程分离。对于轻中度游戏,游戏的通信量不会很多,没必要每个分服都有一个长连接socket网关。假设一个分服同时连接服务器的客户端有5k,一台机器的socket网关能支持5w个玩家。因此网关需要参与服...

js学习-es6实现枚举

最近,我大部分时间都在写dart,突然使用了js。我发现js不能直接声明枚举。目录枚举功能对象冻结()符号实现反映了不可更改值的唯一性。请注意,枚举特性枚举值不能重复,也不能修改。Switchcase可以直接判断对象。冻结()对象。方法可以冻结对象。无法更改实现constEnumSex=Object。冷冻枚举性别。人=1;安慰日志;//符号(男性)表示值co...

Axure RP 8 注册码 更新了

升级8.1.0.3381后,您需要使用以下注册码http://www.raedme.cn/keys/316.htmlLicense:zdfansKey:fZw2VoYzXakllUuLVdTH13QYWnjD6NZrxgubQkaRyxD5+HNMqdr+WZKkaa6IoE5N许可证:zd423Key:LrZoHMetrL7OK8XOVWgvTFn+XOR...