Featured image of post 豆知识(第四篇)

豆知识(第四篇)

快期中了

豆知识(第四篇)

虽然是豆知识,但其实是计算机的知识

document.all属性绕过typeof

document.all 则是 document 下面一个非常老的、兼容历史遗留代码的属性,它返回的是“页面里所有元素”的集合。

它和 document 不是同一个东西:document 是整张文档,document.all 是文档里的元素集合。

1
2
3
4
> typeof(document)
< 'object'
> typeof(document.all)
< 'undefined'

document.all 在 JS 里有一个很怪的历史特性:typeof document.all 会表现得像 undefined。在某些过滤器中会造成误判它,不会把它当成“object”拦掉,反而把它放行了。

下面这条链里是先从 document.all[0] 拿到一个元素,再通过这个元素的 ownerDocument 回到文档对象,最后用 defaultView 拿到 window。

1
2
> document.all[0].ownerDocument.defaultView
< Window {window: Window, self: Window, document: document, name: '', location: Location, }

碎碎念

百合好吃

五一拍的照片

Licensed under CC BY-NC-SA 4.0
你好,这是一个随便写写,随便看看的无聊而与我很重要的网站。
使用 Hugo 构建
主题 StackJimmy 设计