I have a javscript function (actually a jQuery plugin) which I will want to call as either
myFunction("some input");
or
myFunction({ "prop": "value 1", "prop2": "value2" });
How do I, in the function, tell the two apart?
In other words, what should go in the if conditions below?
if (/* the input is a string */)
{
// Handle string case (first of above)
}
else if (/* the input is an object */)
{
// Handle object case (second of above)
}
else
{
// Handle invalid input format
}
I have jQuery at my disposal.
Update: As noted in an answer, if the input is new String('some string'), typeof(input) will return 'object'. How do I test for new String(''), so I can handle that the same way as ''?
jQuery.type may be interesting to you.