⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.07 - lazy instantiation.js

📁 JS设计模式源代码
💻 JS
字号:
/* 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) {      ...    }  };})();/* General skeleton for a lazy loading singleton, step 1. */MyNamespace.Singleton = (function() {  function constructor() { // All of the normal singleton code goes here.    // 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) {        ...      }    }  }  })();/* General skeleton for a lazy loading singleton, step 2. */MyNamespace.Singleton = (function() {    function constructor() { // All of the normal singleton code goes here.    ...  }    return {    getInstance: function() {      // Control code goes here.    }  }})();/* General skeleton for a lazy loading singleton, step 3. */MyNamespace.Singleton = (function() {    var uniqueInstance; // Private attribute that holds the single instance.    function constructor() { // All of the normal singleton code goes here.    ...  }    return {    getInstance: function() {      if(!uniqueInstance) { // Instantiate only if the instance doesn't exist.        uniqueInstance = constructor();      }      return uniqueInstance;    }  }})();

⌨️ 快捷键说明

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