如果我覆盖 `window.URL` 会发生什么 [英] What happens if I override `window.URL`
问题描述
我已经在 JavaScript 中创建了一个自定义 URL 对象,并发现 Firefox 已经在 window.URL
中有一个 MozURL
类型的对象.我没有注意到任何其他浏览器设置window.URL
,所以我想知道以下几点:
I've created a custom URL object in JavaScript and discovered that Firefox already has an object at window.URL
of type MozURL
. I haven't noticed any other browser setting window.URL
, so I'd like to know the following things:
- 用我自己的对象覆盖
window.URL
是否有任何意外后果 window.URL
在 Firefox 中的用途是什么?- 是否有任何其他浏览器也将
window.URL
设置为它们自己的对象1
- Are there any unexpected consequences of overriding
window.URL
with my own object - What is
window.URL
used for in Firefox? - Are there any other browsers that also set
window.URL
to their own object1
我的后备计划当然是使用命名空间对象,但我宁愿能够编写 new URL()
而不是 new foo.URL()
代码>.
My back-up plan is of course to use a namespacing object, but I'd rather be able to write new URL()
instead of new foo.URL()
.
[1] 当然,这可能会随着更高版本的浏览器而改变,所以请记住在任何答案上注明日期
我只是想将对象命名为 URI
而不是 URL
,但即便如此,我仍可能会坚持命名空间以避免将来可能与 发生冲突URI
.
I just had the thought of naming the object URI
instead of URL
, but even so I'll probably stick to namespacing to avoid possible future conflicts with URI
.
推荐答案
它是 Firefox 4 中的新内容,也是 文件 API.
It's new in Firefox 4, and part of a W3C working draft for the File API.
来自文档:
提供用于创建对象 URL 的方法.
Provides methods used for creating object URLs.
你可以改变它;如果您愿意 - 但它可能会成为您以后想要使用的 API.URL
不是他们所期望的,这也可能让其他开发人员感到惊讶.我建议使用命名空间解决方案.
You could change it; if you wanted - but it might become an API that you will want to use later. It might also surprise other developers that URL
isn't what they expect it to be. I'd suggest going with the namespace solution.
这篇关于如果我覆盖 `window.URL` 会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!