The magic trick that makes you crazy in JavaScript _javascript tricks

Source: Internet
Author: User
Tags assert
In the presence of such a variable TT, it satisfies the following code.
After the code executes, errcount=0 and the assertion function never has alert information
The code is as follows:
Copy Code code as follows:
<script>
var tt=/* please define TT * * HERE;
var errcount=0//global variable, used to record the number of assertion declarations that assert functions appear
/*
Assertion function
If V is false, the function is on alert ("Assert error"), and the counter is incremented ErrCount
If V is true, do nothing.
*/
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)//result is print 0

</script>


Magic variables in the same hair csdn:javascript
This question is one of my colleagues to test me after work. I thought for a long time and tried for a long time.
Get the following answers, to meet the requirements of the topic.
The answer is as follows:
var tt=new Object (false);
var tt=new Boolean ();
var tt=new Boolean (false);
Through this topic we can feel the flexibility of JS, but also to deepen the understanding of JS.
I used to use the following judgments in my Code.
if (a) {
Alert (' OK ')
}
Now it seems that the logic of this writing is very big.
Borrow csdn to explain the above problem with a wonderful reply from a netizen
|| This is the operation: start with a meaningful return, or return the last expression (note is not necessarily a Boolean value);
&& is such an operation: Starting from the first, encounter meaningless return, otherwise return the last expression (note above);
! is the operation of: the value of the expression is not (note not the expression).
What is meaningless: The following six 0,null,undefined, "", False,nan
In addition, it is considered meaningful.
New Boolean (), new Boolean (false) is the same thing, because it is an object, it makes sense, but its value is false, so you can see "meaningful false" so that you can explain all the problems.
The new Object (false) is also meaningful, and its value is false, except that its type is Object and the new Boolean () is of type Boolean.
One More reminder: | | And && These two operators are not operations on the value, that is, regardless of the value of the expression during the operation, but the expression itself;
How exactly do these two operators operate on an expression?
Answer: Just say whether the expression is meaningful, regardless of its value geometry.
For expressions, there are only 6 meaningless ones; here's to note: All objects generated in new form are dynamic objects, and dynamic objects are considered meaningful
Another example of two notes:
First, 0| | false| | New Boolean (FALSE)
The operation is as follows:
First: 0 is a constant, just a meaningless one, so continue, and false is meaningless, so continue; new Boolean (false) is a dynamic object, meaningful, so the result of the above operation is new Boolean (false)
Second, 0| | New Boolean (false) | | True
What's the result? Many people will assume the result to be true, but that's not right.
First answer: The result is ditto.
First: 0 is a constant, just a meaningless one, so continue; new Boolean (false) is a dynamic object, meaningful; Here we've found a meaningful expression, so we're not going down. So the result is ditto.
=====
For the && operator, by the same token, no longer say more.
Digression:
Interestingly, after the answer came out, we also used some unconventional means of answering this question (for entertainment only)

var tt=window["assert"]=new Function ();
The meaning of this sentence is equivalent to the following two lines of code
function assert () {}
function tt () {}
JavaScript allows you to repeat the definition of a function, which is executed in the future.
Related Article

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.