0

I was wondering how can i pass two different type of value in a single function which accepts single parameter.

let a function is function a(b){return c;}

i'm trying to pass a string while calling the function a some places like a('a string value') then i'll do something with it. Now i need to pass a html element like a(jquery('#someDiv')) and then i'll some another stuff. I need to differentiate both in the function declaration.

Any idea from the experts?

Thanks in advance.

EDIT:

I was looking for a tip to check the data type i'm going to pass as the parameter of the function a. It can be a string or a number or an object or even a function. I don't want to use a fallback. I want to use the last else statement to through an error or exception.

maksbd19
  • 3,785
  • 28
  • 37
  • if(typeof(b) == 'string'){....} – Rooster May 23 '13 at 20:23
  • 1
    `if (typeof b === "string") { /* Holy cow, it's a string */ } else { /* Maybe its a jQuery object */ };` – Matt May 23 '13 at 20:23
  • 2
    `jquery('#someDiv')` returns a jQuery object, not an HTML element. – Paul May 23 '13 at 20:24
  • [Docs for `typeof`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof). Note that `typeof jQuery('div')` will be `'object'`. – ajp15243 May 23 '13 at 20:24
  • This isn't exactly a duplicate – aaronman May 23 '13 at 20:32
  • you can usually get away with a simple duck type: b.split ? str : obj ; – dandavis May 23 '13 at 20:33
  • Thanks everyone for your effort. What i really am looking for is a tip to distinguish the types of a javascript variable. Here i gave the example using a string and a jquery object. but i want my function to be able to receive any kind of data types. maybe there can be string or number or some html element or some object even some function. I was seeking for a tip to determine any one of them explicitly. But I don't know why and how i'm ended up as a duplicated question. Maybe i didn't explained it in my question. – maksbd19 May 24 '13 at 09:58

1 Answers1

2

The typeof operator can be used to test for strings.

if (typeof b == 'string') {
    //handle string
}

The second part is a bit more tricky. typeof will return object for a jQuery object. I believe it is safe enough to check if the object's [[Prototype]] is the same as the jQuery constructor's prototype using Object.getPrototypeOf:

else if (Object.getPrototypeOf(b) === $.fn) {
    //it is an object that inherits from the jQuery prototype
}

But that isn't supported in IE<9. If a fallback is necessary, instanceof will have a similar effect and can be used instead of Object.getPrototypeOf:

else if (b instanceof jQuery) {}

So a complete solution with old IE support:

if (typeof b == 'string') {
    //handle string
} else if (b instanceof jQuery) {
    //handle jQuery object
} else {
    throw 'Invalid argument';
}

* Note that instanceof will return true even when a constructor "subclasses" jQuery (has a link in the prototype chain to jQuery), while Object.getPrototypeOf will only return true if the object's internal [[Prototype]] points to the same object as the jQuery constructor's prototype. It is rather rare when this will make a difference, but worth keeping in mind.

Nevertheless of these small quirks, this related question has instanceof as the accepted answer, as it is the most compatible solution.

And to explain a couple things, jQuery itself is not a constructor, but its prototype object is shared with jQuery.fn.init which is the actual constructor, and jQuery.fn is a shorthand for jQuery.prototype, so:

jQuery.fn.init.prototype === jQuery.prototype
jQuery.prototype === jQuery.fn

This is going a little off-topic already, so here's a more in-depth article for the interested readers: How jQuery instantiates objects as jQuery.fn.init, and what that means if you want to subclass jQuery

Community
  • 1
  • 1
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • Thanks for your solution. Its very close to the tip i'm looking for. I've edited my question. I hope you'll understand my question once again. Thanks again. – maksbd19 May 24 '13 at 10:06
  • Well, you will have to add a couple more `else if` branches for `typeof b == 'function'` and `typeof b == 'number'`. Either that or do you want to simply identify the kind of object? I fear the latter may not be possible for objects without having constructors/prototypes to test against. – Fabrício Matté May 24 '13 at 10:52
  • Thanks a lot. I've tried this and the the result is current. Thanks a lot for your co-operation. – maksbd19 May 24 '13 at 11:23