Standard builtins' prototypes and toString

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Jun 12 08:34:22 PDT 2014


On Jun 12, 2014, at 8:24 AM, Mark Miller wrote:

> On Thu, Jun 12, 2014 at 8:19 AM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> 
> Fortunately there aren't very many of those. I think it is only Date and RegExp that have this issue among the ES6 built-ins
> 
> WeakMap, Map, Set, others?
Nope, they don't even have custom toString methods.

> 
> The real problem includes ES6 classes as well. Whatever fix we choose, it should apply there as well -- not that I have a concrete proposal. This one's a real puzzler.

By default they just inherit up the prototype chain, typically to Object.prototype.toSring

If a JS programmer chooses to over-ride toString, then it becomes their problem.

The same best practice should be taught to them too, if you are going to do a branding check in a toString method you should fall back to the default object behavior:

class MyClass {
   toString() {
      if (!myBrand(this)) return super.toString();
      ...
    }
}

We can't do this for them because there is no universal branding concept we can apply for them.

Allen



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


More information about the es-discuss mailing list