For your consideration: $type methods
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.
Follow @clientcide on twitter to get notified of new posts.
To follow me personally on twitter, follow @anutron.
January 18th, 2009 at 11:08 am
Would creating a host of is[Type] functions, like PHP, break the JS OO methodology?
January 18th, 2009 at 11:28 am
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.
January 18th, 2009 at 1:50 pm
That’s a cool little trick Aaron!
January 18th, 2009 at 1:52 pm
Comment #3 was me but the OpenID profile didn’t get updated. :P
January 19th, 2009 at 3:18 am
Not sure if you knew Aaron, but String.type(‘hello’) or Number.type(1) already works (same for every other native object).
January 19th, 2009 at 4:28 am
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
January 19th, 2009 at 6:42 am
Note, that there’s also String.type(‘foo’), Number.type() etc. included in MooTools.
January 19th, 2009 at 10:33 am
@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…
January 19th, 2009 at 10:34 am
@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…