5.05 - private methods with closures.js

来自「JS设计模式源代码」· JavaScript 代码 · 共 53 行

JS
53
字号
/* Singleton as an Object Literal. */MyNamespace.Singleton = {};/* Singleton with Private Members, step 1. */MyNamespace.Singleton = (function() {  return {};})();/* Singleton with Private Members, step 2. */MyNamespace.Singleton = (function() {  return { // Public members.    publicAttribute1: true,    publicAttribute2: 10,        publicMethod1: function() {      ...    },    publicMethod2: function(args) {      ...    }  };})();/* Singleton with Private Members, step 3. */MyNamespace.Singleton = (function() {  // Private members.  var privateAttribute1 = false;  var privateAttribute2 = [1, 2, 3];    function privateMethod1() {    ...  }  function privateMethod2(args) {    ...  }  return { // Public members.    publicAttribute1: true,    publicAttribute2: 10,        publicMethod1: function() {      ...    },    publicMethod2: function(args) {      ...    }  };})();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?