In JavaScript, object conversion to Boolean is very simple: all object converts to Boolean are true; even new Boolean (false) Such an object is still true after conversion to Boolean.
Copy Code code as follows:
var x = new Boolean (false);
if (x) {
Console.log ("X is true");
}
When you convert object to string or number, JavaScript calls the two conversion functions of object: ToString () and valueof ().
ToString ()
The ToString () function returns the string representation of object, and the default ToString () method of the object in JavaScript returns the string "[Object]." When you define a class, you can implement a new ToString () method, which returns more readable results. JavaScript defines the more readable ToString () method for array objects, function objects, regular expression objects, and date dates objects:
The 1.array ToString () method returns an array member separated by commas. For example, [1,2,3].tostring () returns the string "1,2,3″.
The 2.function ToString () method returns the text definition of the function. For example, (function (x) {return x*2;}). ToString () returns the string "function (x) {return x*2;}".
The 3.RegExp ToString () method, similar to the ToString () method of a function, returns the literal definition of the regular expression. For example,/\d+/g.tostring () returns the string "/\\d+/g".
The 4.Date ToString () method returns a date-time string with readability.
ValueOf ()
The valueof () function returns the numeric representation of object, and the default valueof () method of the object in JavaScript returns the object itself. As with ToString (), defining a class enables you to implement a new valueof () method that returns the desired result. JavaScript defines a more readable valueof () method for the Date object:
The valueof () method of 1.Date Returns a numeric value that is the time difference, in milliseconds, between the Date object and the January 1, 1970 zero.