MooTools: How to tell if object is array?
Is there a shortcut in开发者_Go百科 MooTools for telling if an object is an object or an array?
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
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
You can do this with native JavaScript:
Object.prototype.toString.apply(value ) === '[object Array]'
Source: The Miller Device
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
精彩评论