Skip to content

Instantly share code, notes, and snippets.

@Ormadont
Last active August 27, 2021 08:53
Show Gist options
  • Save Ormadont/b2d5abbe220a2d91af748427a2019cd6 to your computer and use it in GitHub Desktop.
Save Ormadont/b2d5abbe220a2d91af748427a2019cd6 to your computer and use it in GitHub Desktop.
JS: transform object to primitive
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