4

I know the difference between delegate() and on(), but I have a doubt that on() is very popular and faster than delegate() then why jquery is not removing the delegate() from its library.

live() has drawbacks then it is removed from jQuery-1.9 version. Also if we see the performance between on() and delegate() then on() is much faster than delegate().

Then any reason to keep delegate() in jQuery?

5 Answers 5

3

This is what a member of the jQuery team wrote in regard to this issue in 2013:

"We haven't deprecated .bind or .delegate, nor have we indicated we're removing them at any point.

They're each only one line in the source so size reduction isn't much of a reason to remove them.

We deprecated .live because it has lots of confusing issues that we can't fix."

Basically, while delegate() has been superseded by on(), there are no future plans to deprecate delegate() due to the fact it takes up very little space (and doesn't cause any problems).

2
  • "It takes up very little space" see the live() on code.jquery.com/jquery-1.7.1.js I don't think it also takes too much space then delegate() Commented Apr 7, 2014 at 6:38
  • @RohanKumar True, but as mentioned live() was removed for other reasons
    – dsgriffin
    Commented Apr 7, 2014 at 6:42
3

From the documentation:

As of jQuery 1.7, .delegate() has been superseded by the .on() method.

It is just legacy code. Don't use it.

2
  • Agreed, and saw the code delegate: function( selector, types, data, fn ) { return this.on( types, selector, data, fn );}, in jquery 1.9, so this should be removed from jquery if their is no difference, not? Same question may raise for bind(). Commented Apr 7, 2014 at 6:29
  • "Should be removed" is a somewhat subjective question of backwards-compatibility vs streamlining of the API.
    – Quentin
    Commented Apr 7, 2014 at 6:30
2

I think it is more to do with whether they want to break the users who has used it.... in terms of delegated handlers I don't see any performance diff between them...

So as advised new code should use .on() not .delegate

2
  • So, somehow you agreed with removing delegate() from jquery, and you can check the performance on jsperf.com/jquery-event-delegation/85 Commented Apr 7, 2014 at 6:34
  • @RohanKumar the performance difference is because .on() has to do some conditional checks to see what kind of event has to be registered Commented Apr 7, 2014 at 6:55
2

on is meant to replace delegate. Probably the jquery authors might keep it sometime long to make it more backward compatible. Many old plugins still use those methods. probably that might be the reason to keep it.

2

You can perform both event binding and delegation with the help of on() method. Just the syntax differs.

Just visit this link:

http://learn.jquery.com/events/handling-events/

They have beautifully explained with examples

Not the answer you're looking for? Browse other questions tagged or ask your own question.