Created
December 2, 2014 16:07
-
-
Save jasonbellamy/c18495e09ceb2d471d2d to your computer and use it in GitHub Desktop.
Basic implmentation of a variadic helper function/
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
var variadic = function( fn ) { | |
var _slice = Array.prototype.slice; | |
var _length = fn.length; | |
if ( _length < 1 ) { | |
return fn; | |
} | |
if( _length === 1 ) { | |
return function () { | |
return fn.call( this, _slice.call( arguments ) ); | |
}; | |
} | |
return function () { | |
var namedArguments = _slice.call( arguments, 0, _length - 1 ); | |
var variadicArguments = _slice.call( arguments, _length - 1 ); | |
return fn.apply( this, namedArguments.concat( [variadicArguments] ) ); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment