value_of.md

August 4, 2016 ยท View on GitHub

How to know the valueOf() method Back

In ECMAScript 5, the method valueOf() is defined as followed:

when the valueOf method is called, the following steps are taken:

  1. Let O be the result of calling ToObject passing the this value as the argument.
  1. If O is the result of calling the Object constructor with a host object, then return either O or another value such as the host object originally passed to the constructor. The specific result that is returned is implementation-defined.
  1. return O.

To put it simple, this method will call ToObject(this), and return different things according to this:

  • this is the original object, returning will depends on different browsers.
  • this is not the original object, returning will return the mothod ToObject(this).
Parameter TypesReturn
undefinedThrow TypeError Exception
nullThrow TypeError Exception
NumberCreate a Number Object and pass this into the constructor
StringCreate a String Object and pass this into the constructor
BooleanCreate a Boolean Object and pass this into the constructor
Objectthis
Object TypesReturn
undefinedThrow TypeError Exception
nullThrow TypeError Exception
NumberCreate a Number Object whose value is equivalent to the object itself
StringCreate a String Object whose value is equivalent to the object itself
BooleanCreate a Boolean Object whose value is equivalent to the object itself
ObjectThe Object itself

Example

var num = 123;
console.log(num.valueOf());                    /** => 123           */
console.log(typeof num);                       /** => number        */
console.log(typeof num.valueOf());             /** => number        */

var num = new Number(123);
console.log(num.valueOf());                    /** => 123           */
console.log(typeof num);                       /** => object        */
console.log(typeof num.valueOf());             /** => number        */