Created
January 8, 2011 13:14
-
-
Save robotlolita/770822 to your computer and use it in GitHub Desktop.
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
function classOf(obj) { | |
return Object.prototype.toString.call(obj).slice(8, -1) | |
} | |
// sanitize the arguments for the `_foo` function | |
function foo() { | |
var args = [].slice.call(arguments) | |
, bar, baz | |
// get bar if it's a String | |
if (classOf(args[0]) == "String") bar = args.shift() | |
// baz must be always defined | |
baz = args.shift() | |
// calls the `foo` function with the right arguments it expects | |
_foo.call(this, bar, baz); | |
} | |
function _foo(bar /* String? */, baz /* Anything (required) */) { | |
console.log("bar: ", bar) | |
console.log(baz) | |
} | |
foo("bar"); | |
// >>> bar: bar | |
// >>> undefined | |
foo([1,2,3]); | |
// >>> bar: undefined | |
// >>> [1,2,3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment