forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefineToStringTag.js
31 lines (30 loc) · 1 KB
/
defineToStringTag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
/**
* The `defineToStringTag()` function checks first to see if the runtime
* supports the `Symbol` class and then if the `Symbol.toStringTag` constant
* is defined as a `Symbol` instance. If both conditions are met, the
* Symbol.toStringTag property is defined as a getter that returns the
* supplied class constructor's name.
*
* @method defineToStringTag
*
* @param {Class<any>} classObject a class such as Object, String, Number but
* typically one of your own creation through the class keyword; `class A {}`,
* for example.
*/
export default function defineToStringTag(classObject: Class<any>): void {
if (typeof Symbol === 'function' && Symbol.toStringTag) {
Object.defineProperty(classObject.prototype, Symbol.toStringTag, {
get() {
return this.constructor.name;
},
});
}
}