On the graceful writing of JavaScript

Source: Internet
Author: User
Tags extend

Here is a bit exaggerated, a little hanging, just casually say that they write a graceful way to JS.

Using jquery to develop, now very popular, greatly increased speed, but also can improve the quality of the code.

1, $.extend ()

// ...  --  indicates code    function  demo (opts)        {var defualtopts={...};        OPTs=$.extend ({},opts,defaults);        ...    }     /* use good $.extend () can be very good to pass parameters, look good beautiful */

2, {...}

/* {...} Using objects, this kind of object writing, code reusability is not strong can be used, because no need not to inherit,        but also can be very intuitive.     */    var demo={        init:function() {            this. Action ();            ...        },        action:function() {...},        render:function() { ...},        delegateevent:function() {...}    };

3, o.prototype={}

/* The object-oriented approach is very reusable and object-oriented is very principled, so the code looks good to understand */    function Demo () {        this. v= "...";           This . Init ();    }    Demo.prototype={        Constructor:demo,        init:function() {...},        ...    }

4, the writing of jquery plugin

/*closures-Preventing global pollution*/    (function($,w,undefined) {/*Prevent multiple loads*/        if(!$.fn.demo) {            /*extended jquery, can be $ (..). Demo () to execute*/$.fn.demo=(function(FN) {returnfn (); })(function(){                functionDemo () {Console.log (1); }                returndemo;        }); }}) (jquery,window,undefined);

Now is understood, as to what design pattern, this currently only know its type does not understand its specific use, this is the experience mode, need a lot of experience is experienced.

On the graceful writing of JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.