Skip to content

Instantly share code, notes, and snippets.

@1uokun
Last active March 23, 2019 15:23
Show Gist options
  • Save 1uokun/57c3f5d7d1964ee97d9dc5d1abf4aed1 to your computer and use it in GitHub Desktop.
Save 1uokun/57c3f5d7d1964ee97d9dc5d1abf4aed1 to your computer and use it in GitHub Desktop.
惰性单例简单应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="hello"></div>
<button onclick="open1()">open</button>
<button onclick="close1()">close</button>
<script>
var getSingle = function(fn){
var result
return function(){
return result || ( result = fn.apply(this, arguments) )
}
}
var createLoginLayer = function(){
var div = document.createElement( 'div' )
div.innerHTML = '我是登录浮窗'
div.style.display = ' none '
hello.appendChild( div )
return div
}
//预渲染浮窗
var LoginLayer = getSingle( createLoginLayer )
//打开浮窗
function open1(){
LoginLayer().style.display = 'block'
}
//关闭浮窗
function close1(){
LoginLayer().style.display = 'none'
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment