Implicit conversion of JavaScript

Source: Internet
Author: User
Tags object comparison reference return string string to number tostring

The data type of JavaScript is divided into six types, null,undefined,boolean,string,number,object respectively. object is a reference type, and the other five are of the base type or the original type. We can use the TypeOf method to print out what kind of it belongs to. Variable comparisons of different types have to be typed first, called Type conversions, and type conversions are also called implicit conversions. Implicit conversions usually occur in operator subtraction, equal to, and less than, greater than, etc...

typeof ' One '//string
typeof (one)//number
' One ' < 4//false

Conversion of basic types

Let's talk about subtraction:

1. Strings plus numbers, numbers are converted to strings.

2. Digital subtraction String, string into number. If the string is not a pure number, it is converted to Nan. The same is the case for strings minus numbers. Two string subtraction is also converted to a number first.

3. Multiply, except, greater than, less than the conversion is the same as minus.


Implicit conversions +-* = =/
// +
10 + ' 20 '//' 2010 '
// -
10-' 20 '//-10
-' One '//nan
Ten-' 100a '//nan
// *
10* ' 20 '//200
' 10 ' * ' 20 '//200
// /
20/' 10 '//2
' 20 '/' 10 '//2
'//nan '/' one '



Let's take a look at a group of = =.

1.undefined equals null

2. String to number when comparing strings and numbers

3. When the number is Boolean compared, the Boolean turns the number

4. String and Boolean comparisons, both turn numbers


// ==
undefined = null; True
' 0 ' = 0; True, the string is turned to digits
0 = false; True, Boolean spin digits
' 0 ' = false; True, both turns numbers
NULL = = FALSE; False
undefined = = false; False



Conversion of reference types

The comparison between basic types is relatively simple. The comparison of reference types and base types is relatively complex, and the reference type is first converted to the base type and then compared to the method described above. The reference type Boolean is all true. such as an empty array, as long as the object is the reference type, so [] is true. Reference type a number or a string is either valueof () or ToString (), the object itself inherits Valuof () and ToString (), and valueof () and ToString () are also customized. ValueOf () to a string, number, or itself, depending on the object, and the object must be converted to a string with ToString. The generic object calls valueof () by default.

1. When the object turns a number, call valueof ();

2. When the object turns the string, the call ToString ();

Let's take a look at the following example:

0 = = []; True, 0 = = [].valueof (); -> 0 = 0;
' 0 ' = = []; False, ' 0 ' = = [].tostring (); -> ' 0 ' = = ';
2 = = [' 2 ']; True, 2 = = [' 2 '].valueof (); -> 2 = = ' 2 '-> 2 = 2;
' 2 ' = = [2]; True, ' 2 ' = = [2].tostring (); -> ' 2 ' = = ' 2 ';

[] == !      []; True, [].valueof () = =! Boolean ([])-> 0 = false-> 0 = 0;

When an object is converted to a number, the call to ValueOf () is called ToString (), so I guess the valueof method is this. So the above example 0 = = [] to be changed to the following more reasonable. In any case, [] Finally, it turns into 0.

var valueof = function () {
var str = this.tostring (); Call ToString () first, and turn to a string
//...
}
0 = = []; True, 0 = = [].valueof (); -> 0 = = ' 0 '-> 0 = 0;

Custom valueof () and ToString ();

1. The custom valueof () and ToString () are present, and the valueof () is invoked by default.

2. If only ToString (), call ToString ();

var a = [1];

a.valueof = function () {return 1;}
a.tostring = function () {return ' 1 ';}

A + 1; 2, valueof () first calls

removing valueof () will call ToString ().


var a = [1];

a.valueof = function () {return 1;}
a.tostring = function () {return ' 1 ';}

A + 1; 2, first call valueof ()
Remove valueof
Delete a.valueof;
A + 1; ' 11 ', call ToString ()



What happens if you return to something else?

var a = [1];

a.valueof = function () {return;}
a.tostring = function () {return 1;};

1-a; NaN

Other objects Call valueof () to different types:


var a = {};
A.valueof (); Object {}
var a = [];
A.valueof (); [] Oneself
var a = new Date ();
A.valueof (); 1423812036234 numbers
var a = new RegExp ();
A.valueof (); //    /(?:) /Regular Object





Comparison between reference types is a comparison of memory addresses, and there is no need for implicit conversions, which is not much to say.

[] = = []//false address is not the same

var a = [];
b = A;
b = = a//true


Explicit conversions

Explicit conversions are simpler and can be directly converted using classes as methods.

Number ([]); 0
String ([]); //''
Boolean ([]); True

There is also a simpler way to convert.

3 + '//String ' 3 '
+ ' 3 '//Number 3
!!' 3 '//True



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.