Poiché le specifiche dice così. http://www.ecma-international.org/ecma-262/6.0/index.html#sec-tostring In questa tabella sono definiti i valori di stringa delle primitive. Viene utilizzato solo per gli oggetti ToPrimitive.
La tabella ci dice che ToString
per un oggetto o
è ToString(ToPrimitive(o, "string"))
La specifica ci dice che se ToPrimitive
viene chiamato con un oggetto dobbiamo seguire questi passaggi:
1. If PreferredType was not passed, let hint be "default".
2. Else if PreferredType is hint String, let hint be "string".
3. Else PreferredType is hint Number, let hint be "number".
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
5. ReturnIfAbrupt(exoticToPrim).
6. If exoticToPrim is not undefined, then
a. Let result be Call(exoticToPrim, input, «hint»).
b. ReturnIfAbrupt(result).
c. If Type(result) is not Object, return result.
d. Throw a TypeError exception.
7. If hint is "default", let hint be "number".
8. Return OrdinaryToPrimitive(input,hint).
@@toPrimitive
beeing set è un caso speciale, quindi ora dobbiamo guardare OrdinaryToPrimitive
1. Assert: Type(O) is Object
2. Assert: Type(hint) is String and its value is either "string" or "number".
3. If hint is "string", then
a. Let methodNames be «"toString", "valueOf"».
4. Else,
a. Let methodNames be «"valueOf", "toString"».
5. For each name in methodNames in List order, do
a. Let method be Get(O, name).
b. ReturnIfAbrupt(method).
c. If IsCallable(method) is true, then
i. Let result be Call(method, O).
ii. ReturnIfAbrupt(result).
iii. If Type(result) is not Object, return result.
6. Throw a TypeError exception.
Quindi questo significa che il valore di ritorno di ToPrimitive(o, "string")
è o.toString()
e toString(o.toString())
è lo stesso di o.toString()
.
fonte
2016-06-06 14:06:00
perché la primitiva non ha un metodo 'toString', il suo solo un valore. –
@NinaScholz ma ho pensato che un valore doveva essere forzato a una stringa quando era usato come chiave per un oggetto. Inoltre, dopo aver ridefinito 'Boolean.prototype.toString()', chiamando 'true.toString()' restituisce '" 1 "'. Quindi questo tipo di smentita sostiene che 'true' non ha alcun metodo' toString() '. –
sta stampando 'primitive true, istanza 1'. Non è quello che ti aspetti? –