单例模式
来源:互联网 发布:淘宝品牌授权怎么弄啊 编辑:程序博客网 时间:2024/05/17 08:44
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script><script> /*单例模式 就是方便 模块间的相互调用*/// 外层函数只是为了自动执行,创建函数实例var xw = (function xiaoWang() {//创建类方法var sendMessage = function() {var infoClass = {setLian: function() {this.lian = "大脸";return this;},setZui: function() {this.zui = "大嘴";return this;}}return infoClass;}//创建实例,info这是暴露在外面的,相当于OBJECT-C中的static类变量var info = {createInstance: function() {var singleInstance = null;if (!singleInstance) {singleInstance = new sendMessage();}return singleInstance;}}return info;})();(function xiaoLi() {msg = xw.createInstance().setLian().setZui();console.log(msg);msg = null;}());</script></head><body><button id="btna">按钮a</button><button id="btnb">按钮b</button><script>/*-------------单例模式也可以是 两个全局变量-----------*/var aInstance = {init: function() {this.render();this.binder();},a: 4,render: function() {var me = this;me.btna = $('#btna');},binder: function() {var me = this;me.btna.click(function() {me.test();});},test: function() {bInstance.b = 8;console.log(bInstance);}}var bInstance = {init: function() {this.render();this.binder();},b: 4,render: function() {var me = this;me.btnb = $('#btnb');},binder: function() {var me = this;me.btnb.on('click', (function() {me.test();}));},test: function() {aInstance.a = 7;console.log(aInstance);}}aInstance.init();bInstance.init();</script></body></html>
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- 惰性函数
- 函数颗粒化 参数截取并传递
- 级联函数
- 正则表达式
- JS设计模式书籍、原则
- 单例模式
- 构造函数模式
- JS constructor
- Eclipse启动时发生An internal error occurred during: "Initializing Java Tooling".错误的解决
- JS 工厂模式
- JS 代理模式
- 编码规范
- IOS 中的KVO模式 观察者模式
- JS 职责链模式