10

What is the difference between

var dfd = new $.Deferred

and

var dfd = $.Deferred

In which cases you need to use new vs not using it?

Taryn
  • 242,637
  • 56
  • 362
  • 405
mayrop
  • 681
  • 9
  • 19
  • 1
    I think this is muddied a bit by the behavior of `new`. In particular `new` allows invoking a function without requiring parentheses, so `new $.Deferred` is a shorthand for `new $.Deferred()`. Assigning `$.Deferred` to a variable on the other hand is simply aliasing the original function. – bingles Sep 13 '18 at 18:44

3 Answers3

11

jQuery official documentation says:

"The jQuery.Deferred() constructor creates a new Deferred object. The new operator is optional."

So I guess usage wise, there is not going to be any difference whether you create a new object from Deferred or use it as it is.

zypA13510
  • 1,092
  • 1
  • 10
  • 28
Pratheep
  • 936
  • 1
  • 7
  • 17
  • You answered the question he _should_ have asked. Namely if the second example had been `var dfd = $.Deferred()`. That's a more interesting comparison, and your answer would have been correct. Except the second example in the actual question is nonsense **without the parentheses**, and hardly ever useful. – Bob Stein Sep 04 '17 at 18:18
8

These two are not equal, one creates a diferred object while another creates an alias

var dfd = new $.Deferred

It create a a deferred object instance, for creating an new instance there is no need to use new keyword - you can just say var dfd = $.Deferred()

var dfd = $.Deferred

It create an alias for the type $.Deferred

So I don't see any need to use the second format in anywhere, expect for if you want to create a shortcut. You can use the first format to create a new instance of deferred object

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • 2
    @downvoter don't confuse between `$.Deferred()` and `$.Deferred` they are not equal – Arun P Johny May 14 '13 at 04:27
  • I've never seen `$.Deferred` used as a constructor, always as a factory. I admit it makes more sense with `new`, though. – John Dvorak May 14 '13 at 05:47
  • @JanDvorak still I think the preferred way is to use it as a factory method – Arun P Johny May 14 '13 at 05:50
  • 3
    APJ, I wonder if this might be a good place to state what is returned by all four possibilities; `new $.Deferred`, `new $.Deferred()`, `$.Deferred`, `$.Deferred()`. As it stands, your answer provides 75% of the information. Just needs the last 25%, and presentation in list form or one para per possibility. – Beetroot-Beetroot May 14 '13 at 09:35
  • What is the use case btw them, how will it affect how you use them? – Codler Nov 09 '13 at 11:54
6

These are identical:

var dfd1 = $.Deferred();
var dfd2 = new $.Deferred;
var dfd3 = new $.Deferred();

Each creates a new Deferred object. newis optional because $.Deferred is a factory function.

This is probably nonsense:

var wtf = $.Deferred;

As @ArunPJohny pointed out, it just aliases the factory function. It does not create a Deferred object.

Bob Stein
  • 16,271
  • 10
  • 88
  • 101