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