Last active
August 27, 2021 08:53
-
-
Save Ormadont/b2d5abbe220a2d91af748427a2019cd6 to your computer and use it in GitHub Desktop.
JS: transform object to primitive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function transform(hint) { | |
return hint == 'string' ? this.name : this.height | |
} | |
let man = { | |
name: 'Evgeniy', | |
height: 185, | |
[Symbol.toPrimitive]: transform | |
} | |
let woman = { | |
name: 'Daria', | |
height: 155, | |
[Symbol.toPrimitive]: transform | |
} | |
let animal = { | |
name: 'Ars', | |
height: 20, | |
[Symbol.toPrimitive](hint) { | |
return hint === 'string' | |
? this.name + `(animal)` : this.height | |
} | |
} | |
console.log(man + woman + animal) // expected: 360 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment