Javascrip Modular Mode notation

Source: Internet
Author: User

Module mode closures self-contained modules var Testmodule = (function () {var counter = 0;                    return {incrementcounter:function () {return ++counter;            }, Resetcounter:function () {Console.log ("counter value prior to reset:" + counter);        Counter = 0; }    };}) (); Testmodule.incrementcounter (); Testmodule.incrementcounter (); Testmodule.resetcounter ();//contains namespaces,    Module mode var MyNamespace = (function () {var myprivatevar = 0) for shared and private variables;    var myprivatemehod = function (foo) {console.log (foo);    };            return {mypublicvar: "foo", Mypublicfunction:function (bar) {myprivatevar++;         Myprivatemehod (bar); }}) (); Mynamespace.mypublicfunction (' Hello world! ');    /using modular thinking to realize the shopping cart var Basketmodule = (function () {///private var basket = [];    function Dosomethingprivate () {console.log ("dosomethingprivate run");   } function Dosomethingelseprivate () {console.log ("dosomethingelseprivate run"); }//Returns the shared object of the exposure return {additem:function (values) {Basket.push (values);        }, Getitemcount:function () {return basket.length;            }, Dosomething:dosomethingprivate, Gettotal:function () {var itemCount = This.getitemcount ();            Total = 0;            while (itemcount--) {total+= basket[itemcount].price;        } return total; },}) (); Basketmodule.additem ({item: "Bread", price:0.5}); Basketmodule.additem ({item: "DSS", price:0.5} ); Console.log (Basketmodule.getitemcount ()); Console.log (Basketmodule.gettotal ());

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Javascrip Modular Mode notation

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.