这篇Javascript中发生了什么? [英] What's going on in this piece of Javascript?

查看:79
本文介绍了这篇Javascript中发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

谢谢!

推荐答案

第一个 {} 只是一个诱饵,它被解释为空块并被忽略。所以我们有

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

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

这是

[].constructor.constructor(a)()

这是

Array.constructor(a)()

Function(a)()

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

构造函数 s通过原型解决,这是结构:

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

这篇关于这篇Javascript中发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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