在 JavaScript 中,数组如何引用到空字符串 + true 中是一个有效字符
How is an array reference into an empty string + true a valid character in JavaScript?
我不确定这一行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
一元正减运算符的重要用途是什么?
因为"" + true
是字符串"true"
,而第三个字符(索引2)是u
。
像! + []
这样的事情是有效的,因为+
也可以是一元运算符,请参阅此 SO 问题。
alert( (""+true)[2] ); // shows "u"
它返回字符串"true"的第 3 个字母。
这会带来什么回报?
alert( (''+[[]][[]]));
相关文章:
- 如果URL中有任何字符串变体匹配,则返回true
- 为什么只有当我在字符串周围输入引号时,表单输入的isNaN才为true
- 为什么innerHTML在和相同的字符串值比较时不返回true
- 为什么布尔(true)&&string是javascript中的字符串
- 在HTML中搜索字符串并返回True的Javascript条件
- Javascript TRUE和“;真“;为什么有人使用字符串而不是布尔值
- 比较字符串,如果存在小的拼写错误,则返回 true
- 函数:根据字符串中是否存在子字符串返回 True/False
- 在表的所有 tds 中搜索确切的字符串“已停用”;如果为 true,则隐藏该表的整行
- 在 JavaScript 中,数组如何引用到空字符串 + true 中是一个有效字符
- 如果字符串与正则表达式 JavaScript 不匹配,则返回 True
- 使用 JavaScript 搜索字符串,如果结果为 true,则重定向页面
- 如何将对正在定义或未定义的变量的检查强制转换为读取True或False的字符串
- 如果所有字母都出现在字符串中,则返回 true
- JavaScript字符串比较总是返回true
- 无论传入什么字符串,If语句都始终为true
- 为什么数字<字符串在JavaScript中返回true
- 同时包含字符串true和布尔值true的Javascript
- 仅当某些字符串为 true 时,才根据标题选择 IMG
- 如果上一个字符串为true,则返回false,否则为true-javascript