New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error or undefined result from bad query #31
Comments
it's best to keep the return types consistent. it would break a lot of applications if we returned var results = qwery('#not-found')
for (var i = 0; i < results.length; i++) { // cannot access 'length' of undefined
// do stuff
} It's not say that As it stands, you can always expect to receive an array. if you're concerned if an element exists, you could check the |
Maybe I'm just used to the MooTools way ($ vs $$), but I should know whether I expect a single item or several items. Selectors that search for ids should always return 1 or none, selectors that search for classes, attributes, element names, etc should always return an array. For example:
but
That's what makes sense to me. When i moved from jQuery to MooTools, everything made more sense, and now it's feeling like I'm moving back to jQuery again. I suppose I could make my own plugin, but qwery is such a good selector engine. It would be a shame to reinvent the wheel. |
hmm... |
Unless, of course, the goal was to be like jQuery, in which case it's doing a really good job. I'm just not a big jQuery fan, as you might have guessed. |
it has less to do with "being like jQuery" — but more a general good programming practice whereby you should always expect the same return type from the same method. If you just wanted something by id, and not an array but the element, you would use some other method. In Mootools, that is $$('#hello'); // [<div id="hello"/>]
$$('#not-found'); // [] |
btw, you could always implement a top level method: $.ender({
id: function (s) {
return document.getElementById(s) || null
}
})
$.id('hello'); // <div id="hello"/>
$.id('not-found'); // null |
I guess that makes sense. I might just have to do that. I hear there are MooTools plugins for Ender and that might be a better fit for me. I'm just not a big fan of this style of coding: |
btw, $.id() is available in Bonzo |
Cool, didn't know that. I guess that solves my question. Thanks! |
When selecting something that doesn't exist, why do I get back an object? Shouldn't I get back undefined or see an error?
For example, say I have an element:
<div id='awesome'></div>
I'll select it, but I accidentally mistype it:
var myDiv = qwery('#awesom');
It is really hard to debug this (especially in large applications), and this is one of the reasons why I switched from jQuery.
Errors are nice because they show line numbers and originating files. I think returning undefined in this case makes the most sense.
The text was updated successfully, but these errors were encountered: