Technical Question from Interview

The question is not well stated. I would interpret in a longer-winded way as follows. Callbacks are usually named functions. So you would usually have somethig like:

function buttonClicked((){
}
// somewhere else
let callback = buttonClicked;
createButton(......, callback);

or

createButton(......, this.buttonClicked);

I think the idea here might be to use function scope to create unnamed callback functions dynamically. So for example:

function create ButtonCallbacks(action){
   if(action == "do this"){
     .....
     return function(event){
        .......
      }
    } else if (action == "do that"){
   ....
    return function(event){
       .....
   }
......
}

and then

createButton(....., createButtonCallbacks("do this") )

These “callbacks” would then get the values of their parameters from the scope in which they are called, or else from their argument lists. Every time the button is clicked, the callback would do the same thing. Frankly, I think this would be wonderfully confusing in any but the simplest cases, but having all these named callbacks is no picnic either for someone trying to support the code. So …

Steve

    function once(b, c){
        if(!arguments.callee.num){
            arguments.callee.num = b + c;
        }
        return arguments.callee.num;
    }
    console.log(once(3, 5));//8
    console.log(once(4, 8));//8
        function once(a, b) {
            if (arguments.callee.num || arguments.callee.num === 0) {
                return arguments.callee.num;
            } else{
                arguments.callee.num = a + b;
                return a + b;
            };
        }

how about this function

Have you tried to see this course on Udemy: “JavaScript: Understanding the Weird Parts”?

The teacher represents this concept with many different applications. It is a good course to understand those kind of patterns. I personally liked very much this course. Actually I didn’t finished the series of videos yet, but until now, the lectures are really helping to open my mind to new concepts that only you meet on JS, I think. ^^

Good luck!