There is a JavaScript face question.
f = function () {return true ;}; g = function () {false function () { if (g () && [] = =! = function () {return
false
;}; function g () {return true ;} }}); Console.info (f ());
Look first two lines
var function return true ; }; var function return false; };
These two lines define two variables, not function definitions, and the variables can be re-assigned.
The four lines are more interesting.
(function () { if (g () && [] = =! []) { functionreturnfalse;}; function return true ; } }}) ();
The function g () is called in the If condition, but is this g the function g defined in the second row? The answer is no, because the function g is defined in the anonymous function, and the local definition overrides the external definition, so the intrinsic function g is called and returns True. The first condition passes, entering the second condition.
In fact, the most interesting is the second condition [] = =! [] 。
Look first! [] in JavaScript, when used for Boolean operations, such as here, a non-null reference to an object is treated as true, NULL is considered false. Since this is not a null, it is an array without elements, so [] is considered true, and! The result is false, and if you enter it in the editor of Visual Studio, you will see the prompt immediately, and the expression is always false.
When a Boolean value participates in a conditional operation, true is treated as 1, and false is treated as 0, as shown here: Equality and inequality in Javascript
Now the condition becomes the problem of [] = = 0, when an object participates in a condition comparison, it is evaluated, the result of the evaluation is that the array becomes a string, [] The result is ', ' and ' will be treated as 0, so the condition is set.
The code in the condition is as follows:
function return false ; }; function return true; }
Note that there is no Var in front of F, so, it is not a local variable, so this will be accessed through the closure of the external variable F, re-assignment, now the execution of the F function return value has become false.
As for G, there is no problem, here is a function defined within the G, does not affect the external G function.
The result of the last line will naturally come out, that is, false.