What's going on in this piece of Javascript?

后端 未结 1 1751
误落风尘
误落风尘 2021-01-21 02:06

Can someone explain to me what the following Javascript is doing in terms of the constructors and how it is using / calling the function defined in the variable a?



        
相关标签:
1条回答
  • 2021-01-21 02:36

    The first {} is just a bait, it's interpreted as an empty block and ignored. So we have

    ["apple"]["constructor"]["constructor"](a)()
    

    which is

    [].constructor.constructor(a)()
    

    which is

    Array.constructor(a)()
    

    which is

    Function(a)()
    

    which is

    (function() { alert('Hi') })()
    

    constructors are resolved via prototypes, here's the structure:

    0 讨论(0)
提交回复
热议问题