For your consideration: $type methods

Sunday, January 18th, 2009 @ 10:44 am | filed under: 3rd Party Libraries, Code Snippets, MooTools, Optimization

I don’t know why, but I find myself annoyed very slightly by having to type this often:

if ($type('foo') == 'string') ...

On a lark, I whipped this up:

$type.types = ['string', 'element', 'textnode', 'whitespace', 'arguments',
	 'array', 'object', 'string', 'number', 'date', 'boolean',
	 'function', 'regexp', 'class', 'collection', 'window',
	 'document'].map(function(type){
	$type[type] = function(val) {
		return $type(val) == type;
	};
	return type;
});

This provides the ability to do:

if ($type.string(('foo'))) ...

Which saves me a whopping 3 characters, but feels nicer for some reason. Technically, I should name that method .isString(‘foo’) but whatever.

I don’t think I can bring myself to publish or use this one. I like it, but it just doesn’t seem to be worth it.

9 Responses to “For your consideration: $type methods”

  1. emehrkay Says:

    Would creating a host of is[Type] functions, like PHP, break the JS OO methodology?

  2. Aaron N. Says:

    MooTools makes use of object oriented practices through it’s class inheritance. It has, however, a handful of convenience methods that allow you to accomplish simple tasks that don’t really need any sort of inheritance scheme. $type, $defined, etc. are just there to make writing code easier. By adding a method like the one I have here for each type to see if the object is a match would just me an added convenience, though as I state above, not much of one.

  3. emailtoid.net/i/de2372b5/ Says:

    That’s a cool little trick Aaron!

  4. Rey Bango Says:

    Comment #3 was me but the OpenID profile didn’t get updated. :P

  5. Harald Kirschner Says:

    Not sure if you knew Aaron, but String.type(‘hello’) or Number.type(1) already works (same for every other native object).

  6. Thomas Allmer Says:

    I don’t really like this as for ME it seems to be harder to find out what you are doing…
    $type(‘foo’) == ’string’ => is completely clear for ME that you are trying to find out if it is a string…

    $type.string((‘foo’)) => here I am not really sure I have to think about it (what does it return?, why $type.string), and it makes the code harder to read for ME.

    If you just want to save some chars, how about creating some aliases like
    $isString(‘foo’) => completely clear what you are doing and that is will return just true/false

    this is just MY personal opinion about that, so I won’t use it, but if it helps you it’s surely ok… :p

  7. Jan Kassens Says:

    Note, that there’s also String.type(‘foo’), Number.type() etc. included in MooTools.

  8. Aaron N. Says:

    @harald, @jan, Yeah, I know, but there is no, for instance, Collection.type($$(‘a’)). It only applies to natives. Again, this was something I was contemplating but decided against actually using…

  9. Aaron N. Says:

    @thomas, While I don’t mind using $ methods (I have my own $E and $G methods, for instance) I don’t like to let these things get away from me…