javascript--Package

Source: Internet
Author: User

Encapsulation: The advantage of using object encapsulation is that you can reduce the chance of global variable contamination by speaking of properties, functions are subordinate to an object.

Before encapsulation:

<Script>    varname="Foo"; //name is global and is exposedI=1; //globally, the variables declared without the VAR keyword are global and have little to do with location    functionShow () {//Show is global, exposedConsole.log ("name->"+name); Console.log (++i); }//I is the Global 2Show (); //3Show ();</Script>

Operation Result:

After encapsulation:

Only exposed bar, using closure package    var bar=function () {    var i=1;        return{            Name: "Bar",            show:function () {            console.log ("name->" +this.name);            Console.log (++i);    };}; var bar1=bar ();    2    bar1.show ();    3    bar1.show ();    var bar2=bar ();    2, because it is encapsulated, and the closure, I is the local private    bar2.show ();

Operation Result:

javascript--Package

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.