简体   繁体   中英

MooTools: How to tell if object is array?

MooTools 中是否有快捷方式来判断对象是对象还是数组?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM