(![] + [])[+ []] ...解释为什么会这样 [英] (![]+[])[+[]]... Explain why this works

查看:109
本文介绍了(![] + [])[+ []] ...解释为什么会这样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

alert((![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]);

此代码的输出为: fail 。为什么?

The output of this code is: fail. Why?

顺便说一下,(![] + [])[+!+ []] =='false'[1] ,对吗?但为什么![] + [] ==false以及为什么 +!+ [] == 1

By the way, (![]+[])[+!+[]] == 'false'[1], right?. But why ![]+[] == "false" and why +!+[] == 1 ?

推荐答案

由于@Mauricio评论(![] + [])[+ []] 是f(第一个字符false),(![] + [])[+!+ []])是一个等等...

As @Mauricio commented (![]+[])[+[]] is "f" (the first char of "false"), (![]+[])[+!+[]]) is "a", etc...

它是如何工作的?

让我们检查第一个字符'f ':

Let's examine the first character, 'f':

(![]+[])[+[]]; // 'f'

表达式的第一部分 - 在括号之间 - 由<$ c组成$ c>![] + [] ,Addition运算符的第一个操作数是![] ,它将产生 false ,因为数组对象 - 与任何其他Object实例一样 - 是 truthy ,并应用Logical(!)NOT一元运算符,它产生的值<$ c $例如,c> false 。

The first part of the expression—between parentheses—is composed by ![]+[], the first operand of the Addition operator is ![] and it will produce false, because an array object—as any other Object instance—is truthy, and applying the Logical (!) NOT unary operator, it produces the value false, for example.

![]; // false, it was truthy
!{}; // false, it was truthy
!0;  // true, it was falsey
!NaN;  // true, it was falsey

在它之后,我们有第二个操作数,一个空的数组, [] ,这只是为了将 false 值转换为String,因为字符串表示为空array只是一个空字符串,相当于:

After it, we have the second operand of the addition, an empty Array, [], this is made just to convert the false value to String, because the string representation of an empty array is just an empty string, is equivalent to:

false+[]; // "false"
false+''; // "false"

最后一部分,括号后的方括号,它们是属性访问器,它们接收一个表达式,由Unary Plus运算符再次应用于空数组。

The last part, the pair of square brackets after the parentheses, they are the property accessor, and they receive an expression, which is formed by the Unary Plus Operator applied to an empty array again.

Unary Plus运算符的作用是类型转换, 数字,例如:

What the Unary Plus Operator does is type conversion, to Number, for example:

typeof +"20"; // "number"

再一次,这适用于空数组,正如我所说之前,Array的String表示形式为空字符串,当您将空字符串转换为Number时,它将转换为零:

One more time, this is applied to an empty Array, and as I said before, the String representation of an Array is an empty string, and when you convert an empty string to Number, it is converted to zero:

+[]; // 0, because
+[].toString(); // 0, because
+""; // 0

因此我们可以通过以下步骤解码表达式:

Therefore we can "decode" the expression to in some steps:

(![]+[])[+[]];
(false+[])[+[]];
(false+'')[+[]];
(false+'')[0];
('false')[0];  // "f"

请注意,在String值上使用括号表示法访问字符不属于ECMAScript第3期。版本规范,(这就是 charAt 方法存在的原因)。

Note that accessing characters by using the bracket notation on String values was not part of the ECMAScript 3rd. Edition Specification, (that's why the charAt method existed).

然而,这种索引属性代表字符串的字符在ECMAScript 5上标准化,甚至在标准化之前,该功能在很多浏览器中都可用(即使在IE8(标准模式)中)。

However this kind of "index properties" that represent the characters of a string were standardized on ECMAScript 5, and even before the standardization the feature was available in a good number of browsers (even in IE8 (standards mode)).

这篇关于(![] + [])[+ []] ...解释为什么会这样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆