Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Created January 8, 2011 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotlolita/770822 to your computer and use it in GitHub Desktop.
Save robotlolita/770822 to your computer and use it in GitHub Desktop.
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