Created
October 15, 2015 04:15
-
-
Save platypusrex/c4c7b7c232fae04f0541 to your computer and use it in GitHub Desktop.
Javascript - Arguments and Closures
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 add() { | |
if(arguments.length > 1){ | |
return Array.from(arguments).every(function(val){ | |
return typeof val === 'number'; | |
}) ? Array.from(arguments).reduce(function(prev, current){ | |
return prev + current | |
}) : undefined; | |
}else if(typeof arguments[0] === 'number'){ | |
var x = arguments[0]; | |
return function(y){ | |
return typeof y === 'number' ? x + y : undefined; | |
} | |
}else { | |
return undefined | |
} | |
} | |
//all data types besides number will return undefined | |
//add('2') --> undefined | |
//add(2,'3') --> undefined | |
var addToAdd = add(2); | |
addToAdd(7); // closure example |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment