You are reading a single comment by @DrAzzy and its replies. Click here to read the full conversation.
  • I don't know any references for this - you really do need to understand how functions are defined, called, and used. I threw this together when I saw that there weren't any replies here - hope it's helpful (the meeting I was at was boooring...)

    //these two are the same:
    >function fun1(arg1) {return arg1+1;}
    >var fun1=function(arg1) {return arg1+1;}; //note the semicolon at the end of this - it's an assignment statement. 
    //now fun1 is a function, and you can treat it like any other variable - except that if you put parenthesis after it, that will call the function, and evaluate it appropriately. 
    
    >console.log(fun1)
    function fun1(arg1) {return arg1+1;}
    
    >console.log(fun1()); //will return NaN because undefined+1 is NaN)
    NaN 
    
    

    When you use functions as callbacks you refer to the function; you do not call it. If whatever's calling the callback function is supposed to pass arguments (like setWatch()), it will pass those arguments to the function, assuming the function accepts arguments.

    var func=function(a) {console.log(a);};
    
    setWatch(func, BTN, { repeat:true, edge:'falling' });
    //is the same as:
    setWatch(function(a) {console.log(a);}, BTN, { repeat:true, edge:'falling' });
    
    //Now this won't work, because here you're calling the function, and the value that it returns is used, and now the watch has no callback:
    
    setWatch(func(a), BTN, { repeat:true, edge:'falling' });
    
    //func(a) returns nothing, so it's like you were doing:
    
    setWatch(undefined, BTN, { repeat:true, edge:'falling' });
    
    
About

Avatar for DrAzzy @DrAzzy started