(2) module Mode

Source: Internet
Author: User

The module mode is based on the object literal. The most basic form of object literal is:

VaR car = {};

Implementation of the module Mode Based on Object literal volume:

 var Car = {        color: ‘white‘,        getCarPrice: function () {        },        getCarColor: function () {            console.log(this.color);        }    };    Car.getCarColor();

In the module mode, the private and public methods and variables are implemented.

var testModule = function(){        var privateValue = 1;        var publicValue = 2;        var privateMethod = function(){};        var publicMethod = function(){            console.log(privateValue);        };        var service = {            publicMethod:publicMethod,            publicValue:publicValue        };        return service;    };    testModule().publicMethod();//1    testModule().privateMethod();//undefined is not a function    testModule().privateValue;//no error , just console nothing


Continue to modify

VaR testmodule = (function () {var privatevalue = 1; var publicvalue = 2; var privatemethod = function () {}; var publicmethod = function () {console. log (publicvalue) ;}; var service ={ publicmethod: publicmethod, publicvalue: publicvalue }; return service ;}) (); testmodule. publicmethod (); // 2 testmodule. publicvalue = 3; testmodule. publicmethod (); // 2 cannot be modified,

If you want to modify internal variables, you can only modify them by providing external methods.

var testModule = (function(){        var privateValue = 1;        var publicValue = 2;        var privateMethod = function(){};        var publicMethod = function(){            console.log(publicValue);        };        var setPublicValue = function(value){            publicValue = value;        };        var service = {            publicMethod:publicMethod,            publicValue:publicValue,            setPublicValue:setPublicValue        };        return service;    })();    testModule.publicMethod();//2    testModule.setPublicValue(4);    testModule.publicMethod();//4 

 

(2) module Mode

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.