This commit is contained in:
joaquinelio 2021-06-19 21:21:24 -03:00 committed by GitHub
parent 6f14c398ef
commit 52eaa63aaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,7 @@ In case of such operations, objects are auto-converted to primitives, and then t
That's an important limitation, as the result of `obj1 + obj2` can't be another object!
E.g. we can't make objects representing vectors or matrices (or archievements or whatever), add them and expect a "summed" object as the result. Such architectural feats are automatically "off the board".
E.g. we can't make objects representing vectors or matrices (or achievements or whatever), add them and expect a "summed" object as the result. Such architectural feats are automatically "off the board".
So, because we can't do much here, there's no maths with objects in real projects. When it happens, it's usually because of a coding mistake.
@ -133,7 +133,7 @@ As we can see from the code, `user` becomes a self-descriptive string or a money
If there's no `Symbol.toPrimitive` then JavaScript tries to find methods `toString` and `valueOf`:
- For the "string" hint: `toString`, and if it doesn't exist, then `valueOf` (so `toString` has the priority for stirng conversions).
- For the "string" hint: `toString`, and if it doesn't exist, then `valueOf` (so `toString` has the priority for string conversions).
- For other hints: `valueOf`, and if it doesn't exist, then `toString` (so `valueOf` has the priority for maths).
Methods `toString` and `valueOf` come from ancient times. They are not symbols (symbols did not exist that long ago), but rather "regular" string-named methods. They provide an alternative "old-style" way to implement the conversion.
@ -274,4 +274,4 @@ The conversion algorithm is:
In practice, it's often enough to implement only `obj.toString()` as a "catch-all" method for string conversions that should return a "human-readable" representation of an object, for logging or debugging purposes.
As for math operations, JavaScript doesn't provide a way to "override" them using methods, so real life projects rarely use them on objects.
As for math operations, JavaScript doesn't provide a way to "override" them using methods, so real life projects rarely use them on objects.