Created
December 14, 2015 01:35
-
-
Save mrbobbybryant/1aaae6e087b8c3996337 to your computer and use it in GitHub Desktop.
This function allows you to only call a function if it has everything it needs to run. So it "May" run.
This file contains hidden or 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
| function maybe(fn) { | |
| return function() { | |
| var i; | |
| if ( arguments.length === 0 ) { | |
| return; | |
| } else { | |
| for(i = 0; i < arguments.length; i++) { | |
| if (arguments[i] == null) return; | |
| } | |
| return fn.apply(this, arguments); | |
| } | |
| }; | |
| } | |
| var empty; | |
| function addOne(num) { | |
| return num + 1; | |
| } | |
| console.log( maybe(addOne)(empty) ); | |
| console.log( maybe(addOne)() ); | |
| console.log(maybe( addOne )(1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment