Object.prototype.valueOf = function () { throw new Error('Use an explicit toString'); };What is the effect of this code? You now can’t use the plus operator to convert an object to string, any more:
> var obj = {}; > 'Hello '+obj Error: Use an explicit toString > String(obj) '[object Object]' > obj.toString() '[object Object]' > 'Hello '+String(obj) 'Hello [object Object]'How does this work? To convert an object to a specific primitive type T, it is first converted to any primitive value which is then converted to T. The former conversion happens in two steps [1]:
Without the code snippet at the beginning of this post, Object.prototype.valueOf() returns this (an object) and is inherited by all objects that don’t override this method:
> var obj = {}; > obj.valueOf() === obj trueThe plus operator therefore eventually calls toString(). The code snippet prevents that and throws an error before the operator can get to that method. Note that the error message is not always completely correct:
> Number(obj) Error: Use an explicit toStringBut this trick can still be useful. If an object really wants to be converted to a number then it will bring its own valueOf(), anyway.