0

I have written a simple function that nullifies itself after its first call:

var nullify = function(){
    nullify = null;
    return 1;     
};

If I call it two times, like this:

console.log(nullify());
console.log(nullify);

Then the 1st expression will return 1, and the other will evaluate to null. All clear up to this point.

If I however do it in a single expression and wrap it in a function:

var fun = function(f){
    return f() && !f;
}

Then, for some reason:

console.log(fun(nullify));

evaluates to false, while I would expect it to be true, since 1st operand will return 1 and the other, as a negation of null, true.

The evaluation of the right-hand operand occurs when the nullify functiion has already called nullify = null, is that right? What am I missing?

Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55

1 Answers1

4

What am I missing?

That your fun function is testing the variable f, which still is bound to the function you passed in, not the variable nullify (which has the value null indeed).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375