Why does (![]+[])[+!![]+[]] produce “a”

前端 未结 4 1822
梦谈多话
梦谈多话 2020-12-28 10:01

I am interested in understanding the internals of JavaScript. I\'ve tried to read the source for SpiderMonkey and Rhino but it\'s quite complex to wrap my head around.

4条回答
  •  旧巷少年郎
    2020-12-28 10:19

    If you want to understand why those wierd expressions work as they do, you can open firebug console and experiment yourself. I did and I got that ![] is false, !![] is true, adding an array to a boolean value (false+[] or true+[]) produces a string-version of this value (false+[]="false").

    That way the expression boils down to:

    "false"["1"] 
    

    which is obviously "a"

提交回复
热议问题