Proposal: Symbol Linked to `typeof`

Jordan Harband ljharb at gmail.com
Tue Jan 15 21:35:55 UTC 2019


`.constructor` is unreliable, and can be easily forged - of course,
`Symbol.typeof` would create the same unreliability with one of the few
truly reliable operators in the language.

On Tue, Jan 15, 2019 at 12:38 PM J Decker <d3ck0r at gmail.com> wrote:

>
>
> On Sat, Jan 12, 2019 at 8:19 AM Randy Buchholz <work at randybuchholz.com>
> wrote:
>
>> (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.
>>
>>
>>
>
> can't you just use the constructor name when needing more detail?
>
> var a = new Date();
> a.constructor.name
> "Date"
>
>
>
>> (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] = …
>>
>>>>
>> ```
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190115/3939997e/attachment-0001.html>


More information about the es-discuss mailing list