views:

321

answers:

3

Is there a shortcut in MooTools for telling if an object is an object or an array?

+2  A: 

Not sure about MooTools, but you could check with Javascript:

var someObject = [];
console.log(someObject instanceof Array) // logs true

But since an array is also an object, you'd have to check if it's an Array first before checking for Object. But using the $type method is probably easier.

Edit:

Mootools provides a $type function that gives the type of an object:

Tests ran:

console.log($type("hello"));​​​​​
console.log($type(new Object()));
console.log($type([1, 2, 3]));
​

Output:

string
object
array

Try it before you buy it at http://mootools.net/shell/

Found the info from this article - http://javascript-reference.info/useful-utility-functions-in-mootools.htm

Anurag
The `instanceof` check will return `false` for an array that has come from another window or frame.
Tim Down
+7  A: 

MooTools has a $type(), where you pass in an object.

var myString = 'hello';
$type(myString);

You can find more information at http://mootools.net/docs/core#type

Jay Zeng
A: 

You can do this with native JavaScript:

Object.prototype.toString.apply(value ) === '[object Array]'

Source: The Miller Device

Swingley