Очень интересная тема, на которой я давненько съел собаку при изучении. Теперь хотелось бы помочь закрыть чей-нибудь гештальт при изучении javascript.
Так как поведение внутренних механизмов изменилось со временем, то я обязан учесть этот факт. Итак начнем мы с ядра этого механизма, а затем рассмотрим частные случаи.
Алгоритм ToPrimitive(input[,preferredType]):
The abstract operation ToPrimitive takes argument input (an ECMAScript language value) and optional argument preferredType (string or number). It converts its input argument to a non-Object type. If an object is capable of converting to more than one primitive type, it may use the optional hint preferredType to favour that type. It performs the following steps when called:
1. If Type(input) is Object, then
a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).