简体   繁体   English

这篇Javascript中发生了什么?

[英]What's going on in this piece of Javascript?

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? 有人可以向我解释下面的Javascript在构造函数方面做了什么以及它如何使用/调用变量a中定义的函数?

<script>
a = 'alert("Hi");'
{}["apple"]["constructor"]["constructor"](a)();
</script>

Thanks! 谢谢!

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') })()

constructor s are resolved via prototypes, here's the structure: constructor是通过原型解决的,这里是结构:

在此输入图像描述

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

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