Object conversion functions in JavaScript () valueof () Introduction to _javascript techniques

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.