Proposal: Symbol Linked to `typeof`

Randy Buchholz work at randybuchholz.com
Sat Jan 12 16:19:17 UTC 2019


(Hi all, new to group)

Some of the Well Known Symbols are linked to standard functions -  Symbol.toStringTag - A string value used for the default description of an object. Used by Object.prototype.toString().

When I have a Class instance (say, `const flow = new Flow()`, the debugger shows `flow = Flow {`.
But when I do `console.log( typeof flow)` the output is `object`.

I assume changing basic behavior `typeof` would be breaking, but extending it through a symbol would be useful.

`Symbol.typeofTag` of just `Symbol.typeof`

Invoking `typeof` on an object with this symbol would return "user-typed" information.

(Posting Question)
What is the preferred format/approach for highlighting code in posts?
____________________
Possible Approaches
```
class Flow {
    get [Symbol.typeof]() { return Flow; }  or { return "Flow" }
}
```
would work much like Symbol.species.  Implicitly casting to string would be more intuitive I think.

Other approaches, (special handling of well-knowns) could be:

Decorator-ish
```
[Symbol.typeof] = "Flow"
Class Flow {...
```
or inferred

```
[Symbol.typeof]
class Flow {...
```

Internal - In class, but not in a function.

```
Class Flow {
[Symbol.typeof]
or
[Symbol.typeof] = "Flow"
or
[Symbol.typeof] = Flow
or
[Symbol.typeof] = this

```

Constructor
```
Class Flow {
   Constructor() {
      this[Symbol.typeof] = ...
      ...
```

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190112/ece62fc1/attachment-0001.html>


More information about the es-discuss mailing list