valueof (): Returns the original value that best fits the object type;
ToString (): Returns the original value of the object as a string.
These two methods are generally referred to JS to implicitly call to meet the different operations.
In numerical operations, the valueof (), such as A-b, is invoked preferentially;
In string operations, ToString (), such as alert (c), is invoked preferentially.
1. In JavaScript, there are only a few cases where an error type can cause an error, such as calling a non function, or reading a null or undefined property:
"Hello" (1); Error:not a function
null.x//Error:cannot Read property ' x ' of NULL
2, for example-, *,/, and% arithmetic operators convert operands to numbers, but the "+" number is somewhat different, in some cases it is an arithmetic plus, in some cases a string concatenation symbol, depending on its operand:
2 + 3; 5
"Hello" + "world";//"Hello World"
3, string and number added, the result is a string. JavaScript automatically converts numbers to characters, regardless of whether the number is before or before the string:
"2" + 3; "
2 +" 3 ";//"
1+ "2" +3//"123"
4, NULL will be converted to 0,undefined will be converted to Nan. It should be noted that Nan and Nan are not equal (this is due to the precision of floating-point numbers)
5, JavaScript provides a isnan to detect whether a value is Nan, but this is also not very accurate , because, before invoking the isNaN function, there is an implicit conversion process, which converts values that are not nan to Nan .
isNaN ("foo"); True
isNaN (undefined);//True
isNaN ({});//True
isNaN ({valueof: "foo"});//True
6, only Nan is not equal to their own, the use of unequal number (. = =) To determine whether a number is equal to itself, and to detect Nan
var a = NaN;
A!== A; True
var b = "Foo";
b!== B; False
var c = undefined;
c!== C; False
var d = {};
D!== D; False
var e = {valueof: "foo"};
e!== E; False
function Isreallynan (x) {return
x!== x;
}
7, an object also exists valueof method and ToString method.
All objects inherit two conversion methods:
The first is ToString (), which is the function of returning a string that reflects the object
The second is valueof (), which is to return its corresponding original value.
In General, the conversion of an object to a string passes through the following steps:
1. Call this method if the object has a ToString () method. If it returns an original value, JS converts the value to a string, returning the string result.
2. If the object does not have a ToString () method, or if the method does not return an original value, then JS invokes the ValueOf () method.
3. Otherwise, JS cannot get an original value from ToString () or valueof (), so it throws a type error exception.
in general, in the process of converting objects to numbers, JS does the same thing, but here it first tries to use the valueof () method:
1. If the object has a valueof () method, which returns an original value, JS converts the original value to a number and returns the number.
2. Otherwise, if the object has the ToString () method, and the latter returns an original value, JS converts and returns.
(JS first converts to the corresponding string original value, and then continue to convert the original value to the corresponding number type, and then return the number)
3. Otherwise, JS throws a type error exception.
objects are converted to original values through ToString or valueof methods, the built-in classes in the JS language core try to use valueof () first, and then try to use ToString ()
for all non date objects, the conversion of an object to its original value is essentially a conversion of an object to a number
(The valueof is invoked first, but the date object uses the object to the string conversion mode, but the conversion is immediately used only once, and is not converted to a string and then converted to the corresponding numeric type as described above)
For example, the "+" operator in JS can perform mathematical addition and string concatenation operations.
When one of the operands is an object, JS uses a special method (valueof) to convert the object to the original value instead of using the other arithmetic operators to perform the conversion of the object to the number.
the "= =" operator, "<" and other operators also make objects to the original value of the conversion, but to go out the special case of date objects.
The "-" minus operator converts two operands to numbers.
for date types:
ToString () returns the SAT APR 2015 13:21:08 gmt+0800 form
ValueOf () returns the number of milliseconds
var now = new Date ();
Console.log (now); Date {Sat APR 2015 13:21:08 gmt+0800}
Console.log ((now-1)); 1428124868474
var now = new Date ();
Console.log (now); Date {Sat APR 2015 13:21:08 gmt+0800}
Console.log (typeof (Now+1)); String
console.log ((now+1)); Sat APR 2015 13:21:08 gmt+08001
Console.log (typeof (Now-1)); Number
Console.log ((now-1)); 1428124868474
Console.log (now = = now.tostring ()); True
Console.log (now > Now-1);//true
https://www.zhihu.com/question/24262399/answer/27215444
http://blog.csdn.net/cct418/article/details/50889987