A JavaScript face question

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.