Re: [whatwg/url] Consider adding a constructor to make a URL from parts (#354)

@JAForbes

> Currently that's possible via Object.assign + stringification, but that's a bit clumsy and not unintuitive.

I’m not really sure why you think it’s not intuitive, but I wanted to mention that you don’t need to make a coercion explicitly, as the `URL` constructor does that automatically.

~~~ diff
- let url2 = Object.assign(new URL(url + ""), {pathname: "/about"})
+ let url2 = Object.assign(new URL(url), {pathname: "/about"})
~~~

In addition, URL objects are mutable, and you only need to actually create a new one if you need a copy of the original.

~~~ diff
- let url2 = Object.assign(new URL(url), {pathname: "/about"})
+ url.pathname = "/about"
~~~

If you don’t want to mutate the original record, but don’t need to use it anymore, at least in that function (e.g. it’s a parameter that you don’t want to mutate), you can reassign it instead.

~~~ diff
- let url2 = Object.assign(new URL(url), {pathname: "/about"})
+ url = new URL(url)
+ url.pathname = "/about"
~~~

Personally, I think the approach of using object spreading is a bit clumsy, as new properties could be added to URL records in the future, maybe just to access and assign existing URL data in a different way (e.g. #491).

Also, I think it’s interesting to note that some databases (and/or their drivers) extend the grammar of URLs beyond what this specification recognizes and allows, see e.g. #398.

I’m also not sure whether it’s actually feasible to turn URL record properties into own properties anymore, and even if it is, I don’t think it would be a good idea, as I think it would be kinda inconsistent.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/354#issuecomment-819239182

Received on Wednesday, 14 April 2021 05:31:14 UTC