如何将数组引用为空字符串true是JavaScript中的有效字符?

前端之家收集整理的这篇文章主要介绍了如何将数组引用为空字符串true是JavaScript中的有效字符?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不确定这行 javascript中发生了什么:
  1. alert( (''+[][[]])[!+[]+!+[]] ); // shows "d"

我发现了什么:

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

似乎第二部分是对一系列字母或某种类型的引用,但我不明白它是如何产生的

  1. (''+[][[]])

也:

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

解决方法

我会为你分解它:
  1. ('' + [][[]])[!+[]+!+[]]
  2. = ('' + undefined)[!+[]+!+[]] // [][[]] grabs the []th index of an empty array.
  3. = 'undefined'[! + [] + ! + []]
  4. = 'undefined'[(! + []) + (! + [])]
  5. = 'undefined'[true + true]
  6. = 'undefined'[2]
  7. = 'd'

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

猜你在找的JavaScript相关文章