Convert object in JavaScript to number or string rules Introduction to _javascript Tips

Source: Internet
Author: User

In JavaScript program writing process, according to different contexts, JS will automatically convert object to number or string before processing. The rules for this automatic conversion are as follows:

Object automatically converts to string rules:

1. If the object belongs to a class that is overridden by the ToString () method, the method is called. If the return result of the ToString () call is primitive (string, number, Boolean, undefined, null), the primitive value is converted to a string and returned.

2. If the object belongs to a class that does not overwrite the ToString () method, the return result of the –tostring () call is "[Object]", or the ToString () method is overridden but the method returns the result as an object. Then JS will call the object's valueof () method, if the valueof () call returns the result of primitive (string, number, Boolean, Undefined, null). The primitive value is converted to a string and returned.

3. If the above two points are not satisfied, can not be called the object's ToString () method or valueof () method to get the primitive value, then JS will throw TypeError error.

Object automatically converts to number rules:

1. Call the ValueOf () method of object, and if the primitive value is obtained, the primitive value is converted to number and returned.

2. If the primitive value cannot be obtained from the valueof () method, the ToString () method of object is invoked, and if ToString () returns the primitive value, the primitive value is converted to number and returned.

3. If the above two points are not satisfied, then JS will throw TypeError error.

As you can see, the rules for automatically converting an object into a string and automatically converting the object to number are consistent, except for the order in which the ToString () method and the ValueOf () method are called.

According to the rules above, you can understand some of the transformation results well:


1. For an empty array, when it is converted to number, the result is 0. This is because the valueof () method of the array is invoked first, because valueof () returns the array object itself, so the next JS calls the ToString () method of the empty array, because the empty array ToString () returns the result to an empty string, As a result, the empty string is eventually converted to the number 0 and returned.

2. For an array of only one numeric member, the same rule is converted to number, and the result is the figure.

3. For arrays with multiple numeric members, the resulting result is Nan, because the string cannot be converted to number.

When do I convert to string? When do I convert to number?

When an object is automatically typed, JS chooses to convert it to a string or number, depending on the type of object and the operator's differences. The specific rules are as follows:

1.+ the object on both sides of the operator, converting the object to string.
2. All objects (except the Date object) are converted to number first.
3. For the Date object, precedence is converted to string.

It is noteworthy that for the + operator, in addition to the operator on both sides of the object or string, the rest of the case, the "convert to number" operation, and this operation is also related to the order between the values.

Experiment


Copy Code code as follows:

Console.log (3 * []);//0
Console.log (3 * [2]);//6
Console.log (3 * [1,2]);//nan

Console.log (3 + [2]);//32

var now = new Date ();
Console.log (now + 1)//wed Mar 2014 10:51:39 gmt+0800 (CST) 1
Console.log (now-1);//1395802299223
Console.log (now * 2);//2791604598448

Console.log (True + true);//2
Console.log (2 + null);//2, NULL is converted to 0
Console.log (2 + undefined);//nan, undefined is converted to NaN
Console.log (1 + 2 + "cats");//3 Cats
Console.log (1 + (2 + "cats"));//12 Cats

Related Article

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.