Javascript模块模式的变体
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript模块模式的变体相关的知识,希望对你有一定的参考价值。
var myModule = function () { var publicObj = {}; // returnable object... var privateProperty = "I can be accessed only from within this module"; var privateMethod = function () { // has access to public properties as publicObj.publicVar and private methods } var publicObj.publicProperty = "Publicly accessible as myModule.publicVar."; var publicObj.publicMethod = function () { // Publicly accessible as myModule.publicMethod } return publicObj; }(); var myModule = function(){ var privateMethod = function(){ publicMethod(); } var publicMethod = function(){ return this; } // Copy over methods you want to be public return { publicMethod : publicMethod } }(); var myModule = function(){ // Persists in privateMethods due to closure var that = {}; function privateMethod(){ that.publicMethod(); } return { publicProperty: true, publicMethod: function(){}, // Make this visible in private scope init: function(){ that = this; } }; }().init();
以上是关于Javascript模块模式的变体的主要内容,如果未能解决你的问题,请参考以下文章