Default @@toStringTag for user classes

Claude Pache claude.pache at gmail.com
Tue Dec 2 08:49:02 PST 2014


> Le 2 déc. 2014 à 08:46, Dmitry Soshnikov <dmitry.soshnikov at gmail.com> a écrit :
> 
> Hi,
> 
> Probably worth providing a default implementation of the `@@toStringTag` when evaluating a class [1]. In this case users will be able to do:
> 
> ```
> class Point { ... }
> 
> var p = new Point(1, 2);
> console.log(p); // "[object Point]"
> ```

You seem to imply that  `console.log(p)` will show the result of `p.toString()` in the console. But it is not the case for the majority of browsers.

I've just tried:

```
var Point = function() {}
Point.prototype.toString = function() { return "(this is an object of type Point)" }
console.log(new Point)
```

Results are:

Firefox: Object { }
Chrome: Point{toString: function}
Safari: Object
IE: [object Object]    (this is an object of type Point)

In particular, note that Chrome doesn't need the help of `.toString()` in order to log useful information.

—Claude


More information about the es-discuss mailing list