gpt4 book ai didi

javascript - 对空字符串的数组引用如何成为 JavaScript 中的有效字符?

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:52 25 4
gpt4 key购买 nike

我不确定这行 javascript 中发生了什么:

alert( (''+[][[]])[!+[]+!+[]] ); // shows "d"

我发现了什么:

var a = ! + []; // == true
var b = ! + [] + ! + []; // == 2

似乎第二部分是对字母数组或某种形式的引用,但我不明白这是怎么来的

(''+[][[]])

还有:

alert( (''+[][])[2] ); // nothing happens; console says "unexpected token ]"
alert( (''+[[]][])[2] ); // nothing happens; console says "unexpected token ]"
alert( (''+[[]][[]])[2] ); // shows "d"
alert( (""+true)[2] ); // shows "u"

最佳答案

我帮你分解一下:

  ('' + [][[]])[!+[]+!+[]]
= ('' + undefined)[!+[]+!+[]] // [][[]] grabs the []th index of an empty array.
= 'undefined'[! + [] + ! + []]
= 'undefined'[(! + []) + (! + [])]
= 'undefined'[true + true]
= 'undefined'[2]
= 'd'

! + [] == true 在这里解释 What's the significant use of unary plus and minus operators?

关于javascript - 对空字符串的数组引用如何成为 JavaScript 中的有效字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11074488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com