I'm confused on what to implement, first, my module will use Babel so there are no problems implementing ES6 features, second, I will use the class
construct to create class and not the old prototypal method. So now, I'm confused if I'm going to override toString (which is the old way) or just implement Symbol.toStringTag like what this MDN documentation said, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag
So what is the recommended way?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
They're for different things entirely.
If you're trying to define the string version of your object, provide a
toString
method.If you're trying to add information to your class that
Object.prototype.toString
will use to build its"[object XYZ]"
string, provide a method whose name is the value ofSymbol.toStringTag
.Here's an illustration of the difference:
If we hadn't provided that
get [Symbol.toStringTag]
, that last line would output"[object Object]"
rather than"[object Example]"
.Note that it doesn't have to be a getter, it can be a data property instead. Since you're using Babel, you could define it like this if you include Public Class Fields support (currently Stage 2):
...although of course it would be writable. Alternately: