07.30 "JavaScript"-closures and simple modules

Source: Internet
Author: User

Closures mean that in JavaScript, intrinsic functions always have access to the arguments and variables declared in the external function in which they are located, even after their external functions are returned (end-of-life).

At the same time, to reduce the pollution of global variables

// reduce the pollution  of global variables function outer () {     var a = ten;           function inner () {                   alert (a+ +)                  ;  }  return inner;            }             // alert (a);//a is a local variable that cannot be accessed            in this var test = outer ();             // using closure access a expands the scope of a  

Simple module

varUser = (function(winobj) {vari = 111; varJsonobj = winobj.$ = {                    "Add":function() {alert (' Add user ' +i); },                    "Delect":function() {alert (' Delete user '); },                    "Update":function() {alert (' Modify user '); }                }                returnJsonobj;                        }) (window);                                                            User.add (); varCategory = (function(){                vari = 222; return{                    "Add":function() {alert (' Add product Category ' +i); },                    "Delect":function() {alert (' Delete Product category '); },                    "Update":function() {alert (' Modify Product type ');                        }                }            })(); Category. Add ()

The first module is the addition, deletion of users, and other actions

The second is to do a simple pruning on the basis of the first, and make out the module of the commodity kind

07.30 "JavaScript"-closures and simple modules

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.