JavaScript Expression
This question already has an answer here:
First break out your code to this: !![]
which returns true (!! is to convert to boolean) and now +
converts to number so +!![]
returns 1.
![]
converts to false so +![]
returns 0.
~[]
returns -1
and ~![]
also returns -1
.
~!![]
returns -2
.
Now, -~!![]
returns 2
and -~![]
returns 1.
So, combining them all returns 10162014.
All about you to know is ~, !, +, & -
![] = false;
#
as an expression, it's false due to the !
operatory [] = true;
#
as an expression, it's defined, so it's true
+!![] = 1;
because +true = 1;
+![] = 1;
because +true = 0
, because using a +
operator in JS converts the boolean to an integer ref
So what he's done is basically constructed a numerical value using boolean
to integer
conversion, and some grouping.
[+!![]]+[+![]]+[+!![]]
: []
is an empty array, which is truthy. ![]
is thus false, !![]
is true. +true
forces it to a number, as 1
. Similarly for +![]
as 0
via false
.
[-~!![]+-~!![]-~!![]]
: ~
is a two's complement operator; ~1
is -2
. Thus, this evaluates as -(-2)+-(-2)+-(-2)
, which is 6
.
The remaining addends are analogous.
array + array
will convert arrays to strings; thus [1]+[0]+[1]+[6]...
will give the string "1016..."
The plus at start will convert it to a number.
链接地址: http://www.djcxy.com/p/48050.html上一篇: 为什么打印出10张?
下一篇: JavaScript表达式