2

is there a way to know if a variable passed into a function is a native object? I mean, i have a function that requires only native objects as arguments, for every other type of variable it throws an error. So:

func(Array); //works
func(String); //works
func(Date); //works
func(Object); //works
...
func([]); //Throwr error
func({}); //Throws error

I want to know if there's a way to distinguish between native objects and everything else.

tshepang
  • 12,111
  • 21
  • 91
  • 136
mck89
  • 18,918
  • 16
  • 89
  • 106
  • 1
    Javascript is pretty mushy. Also, `[]` *is* an Array. – Pointy May 12 '10 at 14:23
  • Yes but Array is a function because it's the constructor of the Array object – mck89 May 12 '10 at 14:25
  • Oh I see - you want to operate on the constructor functions themselves. – Pointy May 12 '10 at 14:26
  • I think that empty hash is the shortcut for new Object(). Can you please give a more "detailed" case - why do you need such functionality? I'm sure there's a better solution rather then detecting what is native. – Juriy May 12 '10 at 14:26
  • @Juriy: Yes. `{}` is the same as `new Object()` just as `[]` is the same as `new Array()`. – T.J. Crowder May 12 '10 at 14:27
  • @mck89: Obviously you don't have to answer, but I'd love to know the use case for this... – T.J. Crowder May 12 '10 at 14:28
  • I have a class system implemented in javascript. A class can extend only another class or a native object, so i need to check that the object to extend is a class or a native object – mck89 May 12 '10 at 14:32
  • 1
    @mck89: I think the term *"built-in constructor"* is more accurate than *"native object"* for this case. – Christian C. Salvadó May 12 '10 at 14:34
  • The _types_ Array and Object are _built-in objects_, but not the instances of these, they are _native_. This is defined per the ES262-3 spec. For more info see http://jibbering.com/faq/#objects – Sean Kinsey May 12 '10 at 15:15

3 Answers3

3

You'd have to do an === (or !==) against the list of accepted values (which wouldn't be that long, from your question), being aware that that could be tricked into thinking something wasn't a native that was (just from another window).

But basically:

if (obj !== Array &&
    obj !== String &&
    obj !== Date &&
    /* ...and so on, there are only a few of them... */
   ) {
    throw "your error";
}

Edit Re my comment about things from other windows: Be aware that constructors from one window are not === to constructors from another window (including iframes), e.g.:

var wnd = window.open('blank.html');
alert("wnd.Array === Array? " + (wnd.Array === Array));

alerts "wnd.Array === Array? false", because the Array in wnd is not the same as the Array in your current window, even though both are built-in constructors for arrays.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I didn't know that you can do that type of comparison, thanks. So i think that this is the only way right? There's no way to do a common check for every constructor? – mck89 May 12 '10 at 14:35
  • @mck89: As far as I know, no, there's no shortcut way. (BTW, I edited my answer to explain the window thing better.) – T.J. Crowder May 12 '10 at 15:12
  • this is the most comprehensive answer I have ever received thanks:) – mck89 May 12 '10 at 15:29
2

As far as I know, current "best practice" way to get the type of something is

var theType = Object.prototype.toString.call(theObject);

That'll give you a string that looks like "[object Array]".

Now, keep in mind that [] is an Array instance, and {} is an Object instance.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • If you try to to this with native objects you'll get always [object Function] beacause they are all functions, so there's no way to distinguish them because every other function has the same result – mck89 May 12 '10 at 14:28
  • OK - I typed that under a misunderstanding of your question. Your question title asks about "native objects", when it seems that what you're really interested in is the set of constructor functions that create native objects. See Mr. Crowder's answer. – Pointy May 12 '10 at 14:31
0

There's a "typeof" operator in JavaScript that might help.

alert (typeof arg)

The other (a little more sophisticated) approach is to use

arg.prototype.constructor

this will give the reference to a function that was used to construct the object

Juriy
  • 5,009
  • 8
  • 37
  • 52