kota's memex

It is possible for multiple interfaces to use the same property name for different things. For example, you could define an interface in which the toString method is supposed to convert the object into a piece of yarn. It would not be possible for an object to conform to both that interface and the standard use of toString. This isn't a super common issue, but there exists a solution if it comes up.

Property names are usually strings, but the can also be symbols. Symbols are created with the Symbol function. Unlike strings, newly created symbols are unique.

let sym = Symbol("name")
console.log(sym == Symbol("name")) // false

const toStringSymbol = Symbol("toString")
Array.prototype[toStringSymbol] = function() {
	return `${this.length} cm of blue yarn`
}

console.log([1, 2].toString()) // 1,2
console.log([1, 2][toStringSymbol]()) // 2 cm of blue yarn