I’m learning how to use Mootools’ Class, and I am trying to figure out how to detect whether a passed in element is just a single element (for example, $('foo')), or an Elements instance (e.g. $$('.class')).
I attempted to use typeOf, but both examples return me the type “object”.
I am aware that many Mootools functions can apply to both an Element instance and an Elements instance, but my class needs to know what it is receiving, or else it won’t know to iterate through all passed in elements, for example.
Is there a way I can differentiate between the two?
The
typeOffunction in mootools should be adequate, I would suggest you check your code to make sure the problem isn’t somewhere else. That said, you can also check for the existence of the ‘each’ function, which is a part of the array prototype extended by mootools:However, the mootools function
typeOf(docs: http://mootools.net/docs/core/Core/Core#Core:typeOf) should be giving you the best information. Be sure you aren’t using javascript’s built-in operatortypeof(which I did use in the example above), but the moootools functiontypeOfas follows:See it here: http://jsfiddle.net/49DwN/
Also note, the
typeOfreturn for a group of elements is “element*s*”, where a single element is “element” – note the “s” there. Javascript’stypeofoperator would return “object” for both cases.