擴充Javascript方法

來源:互聯網
上載者:User

今天新學一招,擴充一個方法,注釋寫的很清楚了,直接上代碼

       <script>
        //首先註冊全域變數ShowBook
        if (!window.ShowBook)
            var ShowBook = window.ShowBook = {
                F1: function() {
                    alert(1);
                },
                F2: function() {
                    alert(2);
                },
                Extend: function(dest, source, replace) {//擴充方法
                    for (prop in source) {
                        if (replace == false && dest[prop] != null) continue;
                        dest[prop] = source[prop];
                    };
                    return dest;
                }
            };
           
            (function() {       
                var extendFunction1 = {//待擴充的第一組方法
                    F3: function() {
                        alert(3);
                    },
                    F4: function() {
                        alert(4);
                    }
                };               
                var extendFunction2 = {//待擴充的第二組方法
                    F5: function() {
                        alert(5);
                    },
                    F6: function() {
                        alert(6);
                    }
                };
                //擴充第一組方法
                ShowBook.Extend(ShowBook, extendFunction1, false);
                //擴充第二組方法
                ShowBook.Extend(ShowBook, extendFunction2, false);
            })();

            //調用
            ShowBook.F1();
            ShowBook.F3();
            ShowBook.F5();
    </script>

相關文章

聯繫我們

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