W3C home > Mailing lists > Public > public-script-coord@w3.org > April to June 2013

Re: Interface prototype objects and ES6 @@toStringTag

From: Allen Wirfs-Brock <allen@wirfs-brock.com>
Date: Mon, 13 May 2013 14:44:07 -0700
Cc: "public-script-coord@w3.org" <public-script-coord@w3.org>, es-discuss <es-discuss@mozilla.org>
Message-Id: <1068552A-EE63-4E13-AE59-32B2BAB8C35D@wirfs-brock.com>
To: Erik Arvidsson <erik.arvidsson@gmail.com>

On May 13, 2013, at 2:16 PM, Erik Arvidsson wrote:

> On Mon, May 13, 2013 at 5:01 PM, Allen Wirfs-Brock <allen@wirfs-brock.com> wrote:
> 
> On May 13, 2013, at 1:50 PM, Erik Arvidsson wrote:
> 
>> The way that WebIDL require Object.prototype.toString to return "[object TypePrototype]" for the interface prototype object and "[object Type]" for the instances seems to imply that every instance needs to have an own @@toStringTag.
>> 
>> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.2.4.2
>> http://dev.w3.org/2006/webapi/WebIDL/#es-environment
>> 
>> If an instance does not have its own @@toStringTag, Object.prototype.toString will read through to the [[Prototype]] which would return the wrong string.
> 
> Well, toString just does a [[Get]] for @@toStringTag.  You are perfectly free to implement it as a get accessor that takes into account whether the this value is an instance or a prototype object. Not sure whether the complexity is really worth it in most cases. I considered building something like that into Object.prototype.toString but it seemed hard to justify and there was no (ES) legacy reason for doing so.
> 
> OK. Something like this might work:
> 
> Object.defineProperty(Type.prototype, @@toStringTag, {
>   get: function() {
>     return 'Type' + (this === Type.prototype ? 'Prototype' : '');
>   }
> });
> 
> This is of course observable if we expose @@toStringTag.

@@toStringTag is just a ES code accessible extension mechanism for Object.prototype.toString.  Nobody ever claimed it wasn't observable.

> 
> The preferred way to over-ride toString should be via a toString method, not via @@toStringTag. 
> 
> Agreed, but WebIDL currently mandates the result of Object.prototype.toString.

Maybe it shouldn't.  Part of the motivation here is to start migrating people way from thinking that O.p.toString is a reliable brand test, particularly WRT to anything new that is created.

Allen
Received on Monday, 13 May 2013 21:44:37 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:37:49 UTC