JavaScript立即執行函數學習

來源:互聯網
上載者:User

標籤:blank   瀏覽器   修改   對象   func   sub()   com   module   依賴性   

1.建立對象,方法內變數範圍理解錯誤

        var md1 = {            count: 0,            add: function () {                count++;            },            sub: function () {                count--;            },            show: function () {                console.log(count);            }        };        md1.add();        md1.add();        md1.show();

在不加this的情況之下,會試圖與當前對象的範圍內尋找count。如果硬要這樣寫,就必須加入一個全域變數(或者在上級範圍內有count)count,如下。

        var count = 100;        var md1 = {            count: 0,            add: function () {                count++;            },            sub: function () {                count--;            },            show: function () {                console.log(count);            }        };        md1.add();        md1.add();        md1.show();

當然,正確的方法是如下這樣。

        var md1 = {            count: 0,            add: function () {                this.count++;            },            sub: function () {                this.count--;            },            show: function () {                console.log(this.count);            }        };        md1.add();        md1.add();        md1.show();

但是這樣也不好,會暴露count這個理應是私人的成員,可直接在外部代碼之中修改count,如下

        var md1 = {            count: 0,            add: function () {                this.count++;            },            sub: function () {                this.count--;            },            show: function () {                console.log(this.count);            }        };        md1.add();        md1.add();        md1.count = 100;        md1.sub();        md1.add();        md1.show();

 

2.立即執行函數式,這種方式可以屏蔽count

        var md1 = (function () {            var count = 0;            var add = function () {                count++;            };            var sub = function () {                count--;            };            var show = function () {                console.log(count);            };            return {                add: add,                sub: sub,                show: show            };        })();        md1.add();        md1.add();        md1.count = 100;        md1.sub();        md1.add();        md1.show();

 

3.放大模式,用於向已有模組裡面添加新的公用成員

        var md1 = (function () {            var count = 0;            var add = function () {                count++;            };            var sub = function () {                count--;            };            var show = function () {                console.log(count);            };            return {                add: add,                sub: sub,                show: show            };        })();        var md2 = (function (md) {            md.add2 = function () {                md.add();                md.add();            };            return md;        })(md1);        md1.add();        md1.add();        md1.sub();        md2.add2();        md1.show();        md2.show();

 

4.寬放大模式

        var md1 = (function () {            var count = 0;            var add = function () {                count++;            };            var sub = function () {                count--;            };            var show = function () {                console.log(count);            };            return {                add: add,                sub: sub,                show: show            };        })();        var md2 = (function (md) {            md.add2 = function () {                md.add();                md.add();            };            return md;        })(md1 || {});        md1.add();        md1.add();        md1.sub();        md2.add2();        md1.show();        md2.show();

 

獨立性是模組的重要特點,模組內部最好不與程式的其他部分直接互動。

為了在模組內部調用全域變數,必須顯式地將其他變數輸入模組。

  var module1 = (function ($, YAHOO) {

    //...

  })(jQuery, YAHOO);

上面的module1模組需要使用jQuery庫和YUI庫,就把這兩個庫(其實是兩個模組)當作參數輸入module1。這樣做除了保證模組的獨立性,還使得模組之間的依賴關係變得明顯。這方面更多的討論,參見Ben Cherry的著名文章《JavaScript Module Pattern: In-Depth》。

這個系列的第二部分,將討論如何在瀏覽器環境組織不同的模組、管理模組之間的依賴性。

JavaScript立即執行函數學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.