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?
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.
var dfd1 = $.Deferred();
var dfd2 = new $.Deferred;
var dfd3 = new $.Deferred();
Each creates a new Deferred object. new
is optional because $.Deferred is a factory function.
var wtf = $.Deferred;
As @ArunPJohny pointed out, it just aliases the factory function. It does not create a Deferred object.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With