简体   繁体   English

为什么(([] === [])+ / - /)[1] ='a'和(1 + {})[(1 << 1)+1] ='b'在javascript中?

[英]Why is (([]===[])+/-/)[1] = 'a' and (1+{})[(1<<1)+1] = 'b' in javascript?

Recently I came across an interesting website that illustrates a Javascript Obfuscator: http://bl.ocks.org/jasonsperske/5400283 最近我遇到了一个有趣的网站,它说明了一个Javascript Obfuscator: http//bl.ocks.org/jasonsperske/5400283

For example, (([]===[])+/-/)[1] gives a and (1+{})[(1<<1)+1] gives b . 例如, (([]===[])+/-/)[1]给出a(1+{})[(1<<1)+1]给出b

I have tried hard to understand the evaluation sequence of these obfuscated result but was in vain. 我努力理解这些混淆结果的评估顺序但是徒劳无功。

Taking (1+{})[(1<<1)+1] as an example, I understand that << is the bitwise shift operator and will return 2, so the expression becomes (1+{})[3] . (1+{})[(1<<1)+1]为例,我理解<<是按位移位运算符并将返回2,因此表达式变为(1+{})[3] But then I cannot understand what does it mean by 1+{} and [3] . 但后来我无法理解1+{}[3]是什么意思。

Google isn't really helpful to this problem as search engines don't like the brackets or slashes very much, so in case there are duplicate questions I'm sorry about that. 谷歌对这个问题并不是很有帮助,因为搜索引擎不喜欢括号或斜线,所以如果有重复的问题,我很抱歉。

It's just obfuscation tricks. 这只是混淆技巧。

for example : 例如 :

[]===[] ===> false []===[] ===> false

and

([]===[])+/-/ ===> "false/-/" ( You could test it in the console by yourself) ([]===[])+/-/ ===> "false/-/" (你可以自己在控制台中测试它)

So what is (([]===[])+/-/)[1] ? 那么(([]===[])+/-/)[1]什么? ( second char) (第二个字符)

That's right :'a' 那是对的:'a'

You may want to look at this also : 可能也想看看这个:

在此输入图像描述

1+{} 's result is a string "1[object Object]" , (1+{})[3] is to get the char of index 3 which is b . 1+{}的结果是字符串"1[object Object]"(1+{})[3]是获取索引3的char,即b

The first example: 第一个例子:

[]===[] Comparing two different objects with === , so the result is false , whose toString result is "false" . []===[]使用===比较两个不同的对象,结果为false ,其toString结果为"false"

/-/ is a regex object, whose toString result is "/-/" /-/是一个正则表达式对象,其toString结果为"/-/"

When you do false + /-/ , which will concat using the result of .toString() , so the result will be "false/-/" , and the second char is a . 当你执行false + /-/ ,会使用.toString()的结果进行连接,因此结果将是"false/-/" ,第二个char是a

You could go step by step: 你可以一步一步走:

(([]===[]))

is simply false . 简直是false Converted into a string "false/-/" and indexed by [1] gives you the a of the string "false". 转换成字符串"false/-/"和索引[1]给你a字符串“假”的。

The same goes for (1+{}) which results in the string "1[object Object]" . (1+{}) ,它导致字符串"1[object Object]" And 1<<1+1 is another way of writing 3 so this results in "1[object Object]"[3] , which is simply b . 并且1 << 1 + 1是另一种写3方式,因此这导致"1[object Object]"[3] ,它简单地为b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM