遞迴、閉包、私人變數、特權方法、單例、模組模式(module pattern)

來源:互聯網
上載者:User

標籤:使用   io   ar   資料   cti   amp   sp   on   new   

//使用命名函數運算式實現遞迴var factorial = (function f(num) {    if (num <= 1) {        return 1;    } else {        return num * f(num - 1);    }});//用作塊級範圍(私人範圍)的匿名函數(function(){    var now = new Date();    if (now.getMonth() == 0 && now.getDate() == 1) {        alert("Happy new year!");    }})();//使用建構函式模式實現特權方法的定義function MyObject() {    var privateVariable = 10;//私人屬性    function privateFunction() {//私人函數        return false;    }    //特權方法    this.publicMethod = function() {        }}//使用靜態私人變數實現特權方法(function(){    var privateVariable = 10;//私人屬性    function privateFunction() {//私人函數        return false;    }    //MyObject初始化未經聲明是全域變數     MyObject = function() {};    MyObject.prototype.publicMethod = function() {};})();
/*如果必須建立一個對象並以某些資料對其進行初始化,同時還要公開一下能夠訪問這些私人屬性的方法,那麼就可以使用模組模式。*/var application = function() {    var components = new Array();    components.push(new BaseComponent());    return {        getComponentCount : function() {            return components.length;        },        registerComponent : function(component) {            if (typeof component == "object") {                components.push(component);            }        }    };}


遞迴、閉包、私人變數、特權方法、單例、模組模式(module pattern)

聯繫我們

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