I`m working on a project and I saw that at the end of some functions is called a jquery deferred object like this:
obj && obj.resolve();
What is the purpose of the &&? And what is the purpose of the self placed object in the begining (obj) ?
First I thought that calling like that is something like this:
if obj exists --> then call that function (resolve)...but I`ve tested the code with a simple object and it throws an error:
var b = {
a:function(){alert('ok')}
};
a && a.a(); // ReferenceError: a is not defined
b && b.a(); // works...
The code is saying 'if obj is intantiated and is not null, call obj.resolve()'.
It works due to Javascript treating 0, null and undefined as false, and any other value as true.
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