Skip to content

Commit

Permalink
Use Symbol.toStringTag instead of adding toString to the prototype
Browse files Browse the repository at this point in the history
Closes #21.
  • Loading branch information
domenic committed Apr 29, 2017
1 parent 7d0bf50 commit 0590e5a
Showing 1 changed file with 4 additions and 30 deletions.
34 changes: 4 additions & 30 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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 () {
Expand All @@ -410,7 +385,6 @@ module.exports = {
this.generateMixins();

this.generateOperations();
this.generateToString();
this.generateAttributes();

this.generateRequires();
Expand Down

0 comments on commit 0590e5a

Please sign in to comment.