Skip to content

Commit f5d92f3

Browse files
committed
url: set toStringTag for the URL class
PR-URL: #10562 Fixes: #10554 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 134481d commit f5d92f3

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

lib/internal/url.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ class URL {
222222
parse(this, input, base);
223223
}
224224

225+
get [Symbol.toStringTag]() {
226+
return this instanceof URL ? 'URL' : 'URLPrototype';
227+
}
228+
225229
get [special]() {
226230
return (this[context].flags & binding.URL_FLAGS_SPECIAL) !== 0;
227231
}
@@ -641,15 +645,11 @@ class URLSearchParams {
641645

642646
// "associated url object"
643647
this[context] = null;
648+
}
644649

645-
// Class string for an instance of URLSearchParams. This is different from
646-
// the class string of the prototype object (set below).
647-
Object.defineProperty(this, Symbol.toStringTag, {
648-
value: 'URLSearchParams',
649-
writable: false,
650-
enumerable: false,
651-
configurable: true
652-
});
650+
get [Symbol.toStringTag]() {
651+
return this instanceof URLSearchParams ?
652+
'URLSearchParams' : 'URLSearchParamsPrototype';
653653
}
654654

655655
append(name, value) {
@@ -804,12 +804,6 @@ class URLSearchParams {
804804
}
805805
// https://heycam.github.io/webidl/#es-iterable-entries
806806
URLSearchParams.prototype[Symbol.iterator] = URLSearchParams.prototype.entries;
807-
Object.defineProperty(URLSearchParams.prototype, Symbol.toStringTag, {
808-
value: 'URLSearchParamsPrototype',
809-
writable: false,
810-
enumerable: false,
811-
configurable: true
812-
});
813807

814808
// https://heycam.github.io/webidl/#dfn-default-iterator-object
815809
function createSearchParamsIterator(target, kind) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const URL = require('url').URL;
6+
const toString = Object.prototype.toString;
7+
8+
const url = new URL('http://example.org');
9+
const sp = url.searchParams;
10+
11+
const test = [
12+
[toString.call(url), 'URL'],
13+
[toString.call(Object.getPrototypeOf(url)), 'URLPrototype'],
14+
[toString.call(sp), 'URLSearchParams'],
15+
[toString.call(Object.getPrototypeOf(sp)), 'URLSearchParamsPrototype']
16+
];
17+
18+
test.forEach((row) => {
19+
assert.strictEqual(row[0], `[object ${row[1]}]`,
20+
`${row[0]} !== [object ${row[1]}]`);
21+
});

0 commit comments

Comments
 (0)