ToNumber

Posted zlv2snote

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ToNumber相关的知识,希望对你有一定的参考价值。

true becomes 1 and false becomes 0. undefined becomes NaN, but (curiously) null becomes 0
Objects (and arrays) will first be converted to their primitive value equivalent, and the resulting value (if a primitive but not already a number) is coerced to a number according to the ToNumber rules just mentioned.

To convert to this primitive value equivalent, the ToPrimitive abstract operation (ES5 spec, section 9.1) will consult the value (using the internal DefaultValue operation -- ES5 spec, section 8.12.8) in question to see if it has a valueOf() method. If valueOf() is available and it returns a primitive value, that value is used for the coercion. If not, but toString() is available, it will provide the value for the coercion.

If neither operation can provide a primitive value, a TypeError is thrown.

以上是关于ToNumber的主要内容,如果未能解决你的问题,请参考以下文章