mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
24 lines
410 B
JavaScript
24 lines
410 B
JavaScript
|
/**
|
||
|
* @param {Function} fn
|
||
|
* @return {Function}
|
||
|
*/
|
||
|
var once = function(fn) {
|
||
|
let called = false;
|
||
|
return function(...args){
|
||
|
if (called) {
|
||
|
return undefined;
|
||
|
}
|
||
|
|
||
|
called = true;
|
||
|
return fn(...args);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* let fn = (a,b,c) => (a + b + c)
|
||
|
* let onceFn = once(fn)
|
||
|
*
|
||
|
* onceFn(1,2,3); // 6
|
||
|
* onceFn(2,3,6); // returns undefined without calling fn
|
||
|
*/
|