論javascript編寫優美

來源:互聯網
上載者:User

標籤:init   console   提高   this   經驗   window   efi   alt   快速   

這裡就有點誇張了,有點吊了,就隨便說說自己對js編寫優美的方式。

使用jQuery開發,現在很流行的了,大大增快速度,還可以提高代碼的品質。

1、$.extend()

//...  --  表示代碼    function demo(opts){        var defualtOpts={...};        opts=$.extend({},opts,defaults);        ...    }    /*使用好$.extend()可以很好的傳遞參數,看起好優美*/

2、{...}

/*{...}使用對象,這種對象寫法,對代碼複用性不強可以使用,因為不用不用繼承,        還可以很直觀。    */    var demo={        init:function(){            this.action();            ...        },        action:function(){...},        render:function(){...},        delegateEvent:function(){...}    };

3、o.prototype={}

/*物件導向的方式是複用性很強的,而且物件導向也很有原則,所以代碼看起來好理解*/    function Demo(){        this.v="...";        this.init();    }    Demo.prototype={        constructor:Demo,        init:function(){...},        ...    }

4、jquery外掛程式的寫法

/*閉包方式--防止全域汙染*/    (function($,w,undefined){        /*防止多次載入*/        if(!$.fn.demo){            /*擴充jQuery,可以$(..).demo()去執行*/            $.fn.demo=(function(fn){                return fn();            })(function(){                function demo(){                    console.log(1);                }                return demo;            });        }    })(jQuery,window,undefined);

目前就是現在所瞭解的,至於什麼設計模式,這個目前只知道其型不懂其具體使用,這是經驗模式,需要大量經驗很曆練。

 

論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.