Magic variables that drive you crazy in JavaScript

Source: Internet
Author: User

If such a variable tt exists, it satisfies the following code.
After the code is executed, errCount = 0 and the asserted function has no alert information
The Code is as follows:
 Copy codeThe Code is as follows: <script>
Var tt =/* define tt here */;
Var errCount = 0 // global variable, used to record the number of assert function assertions
/*
Asserted Function
If the value of v is false, alert ("assert error") is used to accumulate the counter errCount.
If v is true, nothing will be done.
*/
Function assert (v ){
If (! V ){
Alert ("assert error ");
ErrCount ++;
}
}

Assert (tt | true) = false)
Assert (tt | false) = false)
Assert (tt & true) = true)
Assert (tt & false) = false)
Assert (true | tt) = true)
Assert (tt | true) = false)
Assert (false | tt) = false)
Assert (tt | false) = false)
Assert (true & tt) = false)
Assert (tt & true) = true)
Assert (false & tt) = false)
Assert (tt & false) = false)

Assert (tt? True: false) = true)
Assert (tt = false) = true)
Assert ((! Tt = tt) = true)
Assert (tt + '') =" false ")
Assert (tt = false)


Alert (errCount) // The result is 0.

</Script>

CSDN: Magic variable in JavaScript
This is a question that one of my colleagues took to test my questions after work. I thought about it for a long time and tried it for a long time.
Obtain the following answer to meet the requirements of the question.
The answer is as follows:
Var tt = new Object (false );
Var tt = new Boolean ();
Var tt = new Boolean (false );
Through this question, we can fully feel the flexibility of js and deepen our understanding of js.
I used to use the following judgment in the code.
If (){
Alert ('OK ')
}
It seems that the logic hidden danger of such writing is very large.
Explain the above problems with the wonderful reply from a netizen in csdn
| This operation starts with a meaningful return. Otherwise, the last expression is returned (not necessarily a Boolean value );
& The operation is like this: starting from the first, a meaningless return is returned; otherwise, the last expression is returned (same as above );
! The operation is as follows: Take the non-value of the expression (note that it is not for the expression ).
What is meaningless? The following six values are 0, null, undefined, "", false, NaN
This makes sense.
New Boolean (), new Boolean (false) is the same thing, because it is an object, it is meaningful, but its value is false, so, it can be viewed as "meaningful false". In this way, all problems can be explained.
New Object (false) is also meaningful. Its value is also false, but its type is Object, while new Boolean () is Boolean.
Another note: | and & are not operations on values, that is, the expressions themselves are operated regardless of the value of the expression;
How do these two operators calculate the expressions?
A: Only whether the expression is meaningful or not, regardless of its value ry.
There are only six meaningless expressions. Note: all objects generated in the new method are dynamic objects, and dynamic objects are considered meaningful.
Here are two examples:
1. 0 | false | new Boolean (false)
The operation is as follows:
First, 0 is a constant, which is just meaningless, so it continues. false is also meaningless, so it continues. new Boolean (false) is a dynamic object and makes sense, therefore, the calculation result is new Boolean (false)
2. 0 | new Boolean (false) | true
What is the result? Many people think that the result is true without looking at it, but this is not correct.
Answer: The result is the same as above.
First, 0 is a constant, which is just meaningless, so proceed; new Boolean (false) is a dynamic object that makes sense; a meaningful expression has been found here, so it is no longer computed. The result is the same as the preceding one.
=====
For the & operator, the same is true.
Digress:
Interestingly, after the answer is answered, we have also adopted some unconventional methods to answer this question (for entertainment only)

Var tt = window ["assert"] = new Function ();
This statement is equivalent to the following two lines of code.
Function assert (){}
Function tt (){}
Javascript allows repeated function definitions. The subsequent execution prevails.

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.