What happens if I override `window.URL`

2019-09-12 12:51发布

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:

  • 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

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] this is, of course, subject to change with later versions of browsers so remember to note the dates on any answers


edit to add:

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.

1条回答
Evening l夕情丶
2楼-- · 2019-09-12 13:29

It's new in Firefox 4, and part of a W3C working draft for the File API.

From the documentation:

Provides methods used for creating object URLs.

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.

查看更多
登录 后发表回答