标签:
代码如下
var single = function (name) { this.name = name; this.instance = null; }; single.prototype.getName = function (){ alert(this.name); }; single.getInstance = function (name) { if( !this.instance ){ this.instance = new single(name); } return this.instance; }; var a = single.getInstance(‘sev1‘); var b = single.getInstance(‘sev2‘); alert( a === b);
var creatDiv = (function () { var instance; var creatDiv = function (html) { if(instance){ return instance; } this.html = html; this.init(); return instance = this; }; creatDiv.prototype.init = function () { var div = document.createElement(‘div‘); div.innerHTML = this.html; document.body.appendChild(div); }; return creatDiv; })(); var a = new creatDiv(‘sev1‘); var b = new creatDiv(‘sev2‘); alert( a === b );
var creatDiv = function (html) { if(instance){ return instance; } this.html = html; this.init(); return instance = this; };
var creatDiv = function () { this.html = html; this.init(); }; creatDiv.prototype.init = function () { var div = document.createElement(‘div‘); div.innerHTML = this.html; document.body.appendChild(div); };
var ProxySingletonCreatDiv = (function () { var instance; return function (html) { if(!instance){ instance = new creatDiv( html ); } return instance; } })(); var a = ProxySingletonCreatDiv(‘sev1‘); var b = ProxySingletonCreatDiv(‘sev2‘); alert( a === b );
var namespace = { a:function(){ alert(1); }, b:function(){ alert(2); } }
var user = (function () { var _name = ‘sven‘, _age = 29; return { getUserInfo : function () { return _name + ‘-‘ + _age; } } })();
Singleton.getInstance = (function () { var instance = null; return function (name) { if(!instance){ instance = new Singleton(name); } return instance; } })();
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <button id="btn">登录</button> </body> <script> var createLogin = function () { var div = document.createElement(‘div‘); div.innerHTML = ‘我是登录弹窗‘; div.style.display = ‘none‘; document.body.appendChild(div); return div; } document.getElementById(‘btn‘).onclick = function () { var login = createLogin(); login.style.display = ‘block‘; } </script> </html>
var createLogin = (function () { var div; return function () { if(!div){ div = document.createElement(‘div‘); div.innerHTML = ‘我是登录弹窗‘; div.style.display = ‘none‘; document.body.appendChild(div); } return div; } })();
var getSingle = function (fn) { var result; return function () { return result || (result = fn.apply(this, arguments)); } }
var createLogin = function () { var div = document.createElement(‘div‘); div.innerHTML = ‘我是登录弹窗‘; div.style.display = ‘none‘; document.body.appendChild(div); return div; };
var bindEvent = function () { $(‘div‘).one(‘click‘, function () { alert(‘click‘); }) }; bindEvent(); bindEvent(); bindEvent();
用getSingle函数也可以达到一样的效果, var bindEvent = getSingle(function () { document.getElementById(‘btn‘).addEventListener( ‘click‘, function () { //原书用的onclick,验证过这个执行几次也是执行一次,改成了事件绑定的模式 console.log(‘ssss‘) }); return true; }); bindEvent() bindEvent()
http://book.douban.com/subject/26382780/
标签:
原文地址:http://www.cnblogs.com/jesse-band/p/5018534.html