views:

85

answers:

2

Today is jQuery day. I found this in the documentation:

blur()   Returns: jQuery Triggers the blur event of each matched element.  
blur(fn) Returns: jQuery Bind a function to the blur event of each matched 
                  element.

In other words, the behavior of the function is totally different depending if it accepts or not an argument.

Is this a design mistake or there's a historical reason for this ?

Keep into account that I know nothing about javascript nor jQuery, and I am trying to get a feeling of it.

+2  A: 

That's how jQuery is designed, it's the same for all events. To add a handler to an element you use e.blur(function(){...}), and to trigger the event, you use e.blur(). It kind of makes sense, you just have to get used to it.

jtbandes
No, it does not make sense at all :)
Stefano Borini
It makes perfect sense.
meder
It does, you just have to interpret `e.blur(function(){...})` as "add a blur handler function to e" rather than "blur e".
jtbandes
I don't see how it can. As far as I see from the current answers, it's not a historical reason. I just found out that javascript has onClick() to bind events, so jQuery goes against this. The point here is that the effect of the function is completely altered, rather than augmented, by the presence of a parameter. IMHO, this is a huge design mistake, and I would reject such code in a review.
Stefano Borini
They're overloaded. Overloading makes sense, doesn't it?
outis
Ok, I can overload foo() to print a nice message, and foo(int) to launch a nuclear attack on elbonia. overloading make sense, its use in this case does not.
Stefano Borini
@Stefano - then use `.bind('blur', function(){})` instead of `.blur(fn)`, if you don't like jQuery try using some of the other libraries or write your own and compare them.
meder
I know I can use bind. My question is relative to understand _why_ this is considered so good to be done even when a blatant design mistake, in particular when non-mistaken solutions exist.
Stefano Borini
Purely for convenience, and I wouldn't regard this as a design mistake. I certainly don't want to type `.bind('blur'` rather than `.bind(fn)` personally.
meder
well, you can always try to post a question "Should two overloaded methods be allowed to behave in a completely different way?" and see what's the general answer from the community :)
Stefano Borini
+1  A: 

Definitely not a design mistake, because it goes for multiple events, such as click, however you should be using .trigger('blur')

It makes sense because .blur() or .click() by nature invokes the event handlers attached to the specific event, and all .blur(fn) does is bind it to .bind('event') where behind the scenes it registers the event handlers.

meder
then I would have a setBlurHandler(fn) method, or more briefly, a onBlur(fn)
Stefano Borini
Do you know how overloading works? And do you realize the reason why `.blur()` exists in the first place is because people have been doing DOM 0 assignment for *years and years and years*, and as a result Resig made his library much more convenient? Do you really wish to go back to the past? I would suggest going through the DOM specification and learning how it works internally, trying to write your own library and use it for a couple months and then comparing it with how jQuery was designed and see which is more convenient.
meder
By `blur()` I meant the overloaded enhanced `blur()` in jQuery obviously.
meder
ok, but overloading should not change the behavior of a method. The two tasks are completely different.
Stefano Borini
Why not? Who says that overloading shouldn't change behavior? Programming hasn't been around for thousands of years like mathematics. We make the laws! we are the pioneers! If you're going to go around making pronouncements like that, you should back them up with reason. So far you've only been acting like it should be *obvious* and why can't anyone else see it? well?! I've met my 15 character minimum now!
Breton
@Breton: because it violates the principle of least astonishment, and if you violate it this way, you produce an unmaintainable mess. You are welcome to make your own laws. Feel free to use goto as well, if you want, but that does not change the point that you are producing horrible code.
Stefano Borini
Here we go, let see what all the others think about it: http://stackoverflow.com/questions/1741632/should-two-overloaded-methods-be-allowed-to-behave-in-a-completely-different-way
Stefano Borini