当前位置: 代码迷 >> 综合 >> [[][[]]+[]][+[]][++[+[]][+[]]] is n in javascript
  详细解决方案

[[][[]]+[]][+[]][++[+[]][+[]]] is n in javascript

热度:48   发布时间:2024-01-19 15:52:22.0
[[][[]]+[]][+[]][++[+[]][+[]]]

This evaluates to "n" in javascript. Why?

Let's start with an empty array

[]

Now, let's access a member of it.

[][]

What member? Let's check for the empty array member

[][[]]

oh, that is undefined. But if we add an empty array to that, it is casted to the string "undefined"

[][[]]+[]

Let us wrap that in an array

[[][[]]+[]]

We can now try to access letters in that string. First, we must unwrap the string. That can be done by accessing the first element of that array.

[[][[]]+[]][0]

0 can be created by casting an empty array to a number:

[[][[]]+[]][+[]]

Now, "n" is the second letter in that string, so we would like to access that:

[[][[]]+[]][+[]][1]

But how can we write 1? Well, we increment 0, of course. Wrap 0 in an array, and increment the first member of it:

++[0][0]

Like before, this is equivalent to

++[+[]][+[]]

So our final code is then the glorious

[[][[]]+[]][+[]][++[+[]][+[]]] [[][[]]+[]] [+[]][++[+[]] [+[]]] 这在javascript中评估为“n”。 为什么? 我们从一个空数组开始 [] 现在,让我们访问它的成员。 [] [] 什么成员? 我们来检查一下空的数组成员 [] [[]] 哦,这是未定义的。 但是,如果我们添加一个空数组,它被转换为字符串“undefined” [] [[]] + [] 让我们把它包装在一个数组中 [[][[]]+[]] 我们现在可以尝试访问该字符串中的字母。 首先,我们必须打开字符串。 这可以通过访问该数组的第一个元素来完成。 [[][[]]+[]] [0] 可以通过将一个空数组转换为数字来创建0: [[][[]]+[]] [+[]] 现在,“n”是该字符串中的第二个字母,因此我们想访问: [[][[]]+[]] [+[]] [1] 但是我们怎么能写1? 那么当然我们增加0。 在数组中包装0,并增加其中的第一个成员: ++[0] [0] 像以前一样,这相当于 ++[+[]] [+[]] 那么我们的最终代码就是光荣的 [[][[]]+[]] [+[]][++[+[]] [+[]]]

  相关解决方案