3

For example:

function foo (parameter1, parameter2) {
    // does something
}

if (foo) {
    foo(parameter1, parameter2);
}

Calling a function without parentheses was talked about in a different question (In JavaScript, does it make a difference if I call a function with parentheses?), but I still don't understand what the above code does.

I'm asking because I'm trying to understand what if (drag) {..} does in this code: https://andreasrohner.at/posts/Web%20Development/JavaScript/Simple-orbital-camera-controls-for-THREE-js/

H Gent
  • 35
  • 7
  • if (foo exists) call foo() – mplungjan Dec 08 '18 at 18:48
  • 1
    The function object, when it exists and is defined as some function, is a truthy value that will count as a positive for the if conditional. If the function didn't exist, it would throw an Uncaught ReferenceError. – George Pantazes Dec 08 '18 at 18:49
  • Why don't you try it and find out? – j08691 Dec 08 '18 at 18:50
  • https://www.google.nl/search?q=javascript+detect+existence+of+function+site:stackoverflow.com – mplungjan Dec 08 '18 at 18:51
  • 1
    This isn't a great idea. It only tests that `foo` exists and is *truthy*. `foo` can still be anything and if it's not a function it will throw and error. Also if `foo` is not defined at all, this is a reference error. – Mark Dec 08 '18 at 18:52

3 Answers3

2

Your code defines a global function foo with two parameters.

After that there's an if statement to check if foo is truthy(foo is your function and therefore truthy). So it calls the function foo. But be careful, if foo was not defined your code will throw a ReferenceError.

A much better way to check if a function exists is:

if (typeof foo === "function") { 
  // safe to use the function foo
} 
wuarmin
  • 3,274
  • 3
  • 18
  • 31
  • 1
    It seems to me that what OP is actually asking is what the *purpose* of this `if` statement might be. If the function wasn't defined, that `if` statement will throw the same `ReferenceError` as just trying to call it right away. So if the purpose is not to check for the existence of the function, what could it be? – Berthur Dec 08 '18 at 19:04
0

This code simply check if ~~a function~~ something named foo exists ~~, and therefore safe to call~~.

On the other hand, if you do if (foo()), the code inside that if block will only execute if foo() return something equate to true.

Derek Nguyen
  • 11,294
  • 1
  • 40
  • 64
  • 2
    Safe to call might be an exaggeration. This only tests whether `foo` is truthy, not whether it’s a function. – Mark Dec 08 '18 at 18:50
0

By itself, it's not clear how this code is supposed to be helpful. Maybe with more context it would be clear.

Consider these options:

1. The function foo was never defined:

If foo is not declared then:

if(foo) {} 

is a reference error. It crashes.

2. Foo is defined, but it isn't a function

foo can be anything, so if you have foo that looks like this:

let foo = true
if(foo) {
   foo()
}

It's an error because you're trying to call something that's not a function.

So what exactly is this piece of code trying to test for? It won't call the function if the name foo is declared and equals some falsy value like 0 or undefined, but in other cases it will crash. It looks like it is trying to protect you from errors, but it doesn't really unless there's some other context where the name is declared but may be undefined.

A common context where foo would be guaranteed to be declared is inside a function where foo is an argument. The code would make more sense in an example like:

someFunction(foo){
    if(foo) {
       foo()
    }
}

Here you can expect foo to exist, but you still need to trust that the caller passed a function. This would guard against a situation where the called does not pass in an argument. (i.e. calling someFunction())

Mark
  • 90,562
  • 7
  • 108
  • 148
  • The code [here](https://andreasrohner.at/posts/Web%20Development/JavaScript/Simple-orbital-camera-controls-for-THREE-js/), under Browser Mouse Events, calls the `drag(deltaX, deltaY) {...}` function when the mouse moved, and so the only argument passed in the event, so I'm not sure why the author did this – H Gent Dec 08 '18 at 19:22
  • @HGent in the context of that code, it looks like simple defensive programming. `addMouseHandler` is a function that takes a `drag` parameter. Maybe that parameter is optional, so if it isn't passed to the function, it won't try to call it. – Mark Dec 08 '18 at 19:56