4

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

4 Answers 4

9

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

1
2

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

1
  • The instanceof check will return false for an array that has come from another window or frame.
    – Tim Down
    Commented Jan 27, 2010 at 9:37
1

You can do this with native JavaScript:

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

Source: The Miller Device

0

In version 1.3.2 and above you can use the typeOf function, there is also a shorter and more sementic shortcut using the Type object:

// syntax Type.is[type]

Type.isArray(['foo', 'bar']); // true

Not the answer you're looking for? Browse other questions tagged or ask your own question.