1

I am working on someone script and I don't understand following line. Can someone explain me this line?

 var self = this,
 "function" == typeof self.options.callback && self.options.callback.call(self)
 callback: function() { .. }

options is object

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marmik Desai
  • 104
  • 11
  • Look like a conditional statement but used as an expression! – Afzaal Ahmad Zeeshan Jul 15 '14 at 18:24
  • Thankx for quick replay, but how that function call when i remove this line from script I am getting error. There is callback function but I am not sure how that function call here? Can you explain me this? – Marmik Desai Jul 15 '14 at 18:26
  • @hindmost: No, it really doesn't. – Bergi Jul 15 '14 at 18:28
  • For the odd code, see the marked duplicate. If you don't understand what the `("function" == typeof self.options.callback)` part means, check the [MDN docs on the `typeof` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) – Bergi Jul 15 '14 at 18:30
  • when I console "self.options.callback" this line i am getting callback function, but when i console "self.options.callback.call(self)" this line i am getting undefined. Addition if I remove "self.options.callback.call(self)" this my script doesn't work... – Marmik Desai Jul 15 '14 at 18:32
  • @Bergi i am not asking about && operator – Marmik Desai Jul 15 '14 at 18:42
  • @simpleclick: Uh, [`.call(self)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) just calls that function and returns the result? What else is it that you are asking about? – Bergi Jul 15 '14 at 20:11

1 Answers1

1

It is verifying that the type of self.options.callback is a function and that self.options.callback.call(self) is resolved to true.

From MDN

The typeof operator returns a string indicating the type of the unevaluated operand.

Benjamin Trent
  • 7,378
  • 3
  • 31
  • 41