js31---觀察者模式

來源:互聯網
上載者:User

標籤:blog   code   模式   調用   already   body   執行   html 4.01   函數類型   

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        <title>Untitled Document</title>        <script type=text/javascript charset=utf-8>                                //發行者(被觀察者)        var Publish = function(name){            this.name = name ;            this.subscribers = [];//所有的訂閱者數組(每一個元素是函數類型fn)        };                //發行者發布訊息        Publish.prototype.deliver = function(news){//當成員函數看            var publish = this ;            this.subscribers.forEach(function(fn){//fn是遍曆過程中每一個元素                fn(news,publish);    //把新訊息發給一個訂閱者            });            return this ; //return this 可以繼續調用本函數或其他函數        };                Function.prototype.subscribe = function(publish){//當成員函數看            var sub = this;            //some:數組的每一個元素,如果其中有一個返回true,那麼整體返回true            var alreadyExists = publish.subscribers.some(function(item){                return item === sub ;             });            //如果當前出版社裡不存在這個人 則將其加入其中            if(!alreadyExists){                 publish.subscribers.push(this);            }            return this; //return this 可以繼續調用本函數或其他函數        };                Function.prototype.unsubscribe = function(publish){//當成員函數看            var sub = this ;             // filter (過濾函數:迴圈便利數組的每一個元素,執行一個函數如果不匹配,則刪除該元素)            publish.subscribers = publish.subscribers.filter(function(item){                return item !== sub ;            });            return this; //鏈式編程        };                //發行者對象        var pub1 = new Publish(‘第一報社‘);                //觀察者(訂閱者)        var sub1 = function(news){ //sub1當成函數地址,當成普通變數看,也就是sub1類型是函數類型而已。            alert(arguments[1].name + ‘:‘ + news + "sub1");         };        var sub2 = function(news){             alert(arguments[1].name + ‘:‘ + news + "sub2");         };                sub1.subscribe(pub1);//訂閱就是加入到發行者的數組中        sub2.subscribe(pub1);                pub1.deliver(‘text1‘);                sub1.unsubscribe(pub1); //取消訂閱        </script>    </head>    <body>    </body></html>

 Function.prototype:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        <title>Untitled Document</title>        <script type=text/javascript charset=utf-8>        var Publish = function(name){            this.name = name ;        };                Function.prototype.subscribe = function(){  //載入函數類型的變數上            alert(123);        };                var g = function(){}        g.subscribe();//123                var f = new Publish("sss");        f.subscribe();//f.subscribe is not a function                function h(){}        h.subscribe();//123                </script>    </head>    <body>    </body></html>

 

js31---觀察者模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.