From 0590e5a8b3e91edf39d40209795ff41b87787bfe Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Mon, 24 Apr 2017 17:06:30 +0500 Subject: [PATCH] Use Symbol.toStringTag instead of adding toString to the prototype Closes #21. --- lib/constructs/interface.js | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/lib/constructs/interface.js b/lib/constructs/interface.js index ff2e6368..3b807686 100644 --- a/lib/constructs/interface.js +++ b/lib/constructs/interface.js @@ -66,11 +66,9 @@ const IteratorPrototype = Object.create(utils.IteratorPrototype, { enumerable: true, configurable: true }, - toString: { - value: function toString() { - return "[object ${this.name}Iterator]"; - }, - writable: true, + [Symbol.toStringTag]: { + value: "${this.name}Iterator", + writable: false, enumerable: false, configurable: true } @@ -357,30 +355,6 @@ Interface.prototype.generateAttributes = function () { } }; -Interface.prototype.generateToString = function () { - let hasToString = false; - for (const member of this.idl.members) { - if (member.stringifier || member.name === "toString") { - hasToString = true; - } - } - if (!hasToString) { - this.str += `\n${this.name}.prototype.toString = function () { - if (this === ${this.name}.prototype) { - return "[object ${this.name}Prototype]"; - }`; - if (this.idl.inheritance) { - this.str += ` - return ${this.idl.inheritance}.interface.prototype.toString.call(this);`; - } else { - this.str += ` - return this[impl].toString();`; - } - this.str += ` -};\n`; - } -}; - Interface.prototype.generateSymbols = function () { const unscopables = {}; for (const member of this.idl.members) { @@ -393,6 +367,7 @@ Interface.prototype.generateSymbols = function () { this.str += ` ${this.name}.prototype[Symbol.unscopables] = ${JSON.stringify(unscopables, null, ' ')};\n`; } + this.str += `\n${this.name}.prototype[Symbol.toStringTag] = ${JSON.stringify(this.name)};\n`; }; Interface.prototype.generate = function () { @@ -410,7 +385,6 @@ module.exports = { this.generateMixins(); this.generateOperations(); - this.generateToString(); this.generateAttributes(); this.generateRequires();