Created
March 20, 2017 09:09
-
-
Save yy-dev7/d59dcef4693db22223f29eba0a4f42bb to your computer and use it in GitHub Desktop.
getSingle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* [惰性单例] | |
* @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