Skip to content

Instantly share code, notes, and snippets.

@yy-dev7
Created March 20, 2017 09:09
Show Gist options
  • Save yy-dev7/d59dcef4693db22223f29eba0a4f42bb to your computer and use it in GitHub Desktop.
Save yy-dev7/d59dcef4693db22223f29eba0a4f42bb to your computer and use it in GitHub Desktop.
getSingle
/**
* [惰性单例]
* @param {Function} fn [需要使用单例模式的函数]
* @return {Function}
*/
function getSingle(fn) {
var ret
return (...args) => ret || (ret = fn.apply(this, args))
}
/**
* Usage
*
* var createLoginLayer = function() {
var div = document.createElement('div')
div.innerHTML = 'loginLayer'
div.style.display = 'none'
document.body.appendChild(div)
return div
}
var createSingleLoginLayer = getSingle(createLoginLayer)
loginLayer只会被创建一次
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment