UNPKG

821 BJavaScriptView Raw
1/**
2 * The `defineToStringTag()` function checks first to see if the runtime
3 * supports the `Symbol` class and then if the `Symbol.toStringTag` constant
4 * is defined as a `Symbol` instance. If both conditions are met, the
5 * Symbol.toStringTag property is defined as a getter that returns the
6 * supplied class constructor's name.
7 *
8 * @method defineToStringTag
9 *
10 * @param {Class<any>} classObject a class such as Object, String, Number but
11 * typically one of your own creation through the class keyword; `class A {}`,
12 * for example.
13 */
14export default function defineToStringTag(classObject) {
15 if (typeof Symbol === 'function' && Symbol.toStringTag) {
16 Object.defineProperty(classObject.prototype, Symbol.toStringTag, {
17 get: function get() {
18 return this.constructor.name;
19 }
20 });
21 }
22}