doc: improve WHATWG url constructor code example

Currently, the URL docs for the WHATWG URL spec support are
somewhat lacking in their code example of how to access the
new URL constructor that lives inside the core url package.

PR-URL: https://github.com/nodejs/node/pull/32782
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Liran Tal authored and Ruben Bridgewater committed 2020-04-28 13:58:31 +02:00
1 parent bd40a8b3d7
commit de3ab9f576
1 file changed
+7
+7
View File
@@ -105,6 +105,13 @@ const myURL = new URL('/foo', 'https://example.org/');
// https://example.org/foo
```
The URL constructor is accessible as a property on the global object.
It can also be imported from the built-in url module:
```js
console.log(URL === require('url').URL); // Prints 'true'.
```
A `TypeError` will be thrown if the `input` or `base` are not valid URLs. Note
that an effort will be made to coerce the given values into strings. For
instance: