Skip to content

Instantly share code, notes, and snippets.

@KJlmfe
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KJlmfe/11178189 to your computer and use it in GitHub Desktop.
Save KJlmfe/11178189 to your computer and use it in GitHub Desktop.
JavaScript 单体模式-最基本结构的单体
/* JavaScript 单体模式 - 最基本结构的单体 */
var Singleton = {
attribute1: "public attribute 1",
method1: function() {
console.log("This is public method1");
// 不保险 若Singleton.method1作为一个事件监听器,那么this就会指向window
console.log(this.attribute1);
// 最保险
console.log(Singleton.attribute1);
}
};
// This is public method1
// public attribute 1
// public attribute 1
Singleton.method1();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment