Home  >  Article  >  Web Front-end  >  A brief analysis of implicit type conversion in JavaScript_javascript skills

A brief analysis of implicit type conversion in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:10:441032browse

If you explicitly convert a certain type to another type through a function or method call, it is called explicit conversion, and the opposite is called implicit type conversion. The words "display type conversion" and "implicit type conversion" are not found in Google and Wikipedia. Let’s call it that.

1. Implicit type conversions in operations

1, " " operator

Copy code The code is as follows:

var a = 11, b = '22';
var c = a b;

Here the engine will first turn a into the string "11" and then connect it with b , becomes "1122". Some people may wonder, why not turn b into the number 22 and then perform arithmetic addition, in which case c will be 33. There is no reason why, when one on both sides of the operator " " is a numeric type and the other is a string type, the js engine stipulates that the string concatenation operation be performed instead of the arithmetic addition operation. Using the feature of operator " ", Number can be easily converted into String. For example,
Copy code The code is as follows:

var a = 11;
alert(typeof a); //-->number
a = a '';
alert(typeof a); //-->string

2," -" operator

"-" can be a unary operator (negative) or a binary operator (subtraction operation). For example,

Copy code The code is as follows:

var a = 11, b = '5';
var c = a - b;
alert(typeof c); //--> number

This is contrary to the above " ", and the string b will be implicit is converted into the number 5 and then arithmetic subtraction is performed. Using this feature, you can easily convert String into Number
Copy code The code is as follows:

var a = '11';
a = a - '';
alert(typeof a);// -->number

two , implicit type conversions in statements

1, if

Copy code The code is as follows:

var obj = {name:'jack'}
if(obj){
//do more
}

Here obj will be implicitly converted to Boolean type

2, while

Copy code The code is as follows:

var obj = {name:'jack'}
while(obj){
//do more
}

Same as if

3, type conversion when for in
Implicit conversion from identifier to string occurs when defining an object literal.

Copy code The code is as follows:

var person = {'name':'jack', "age":20,school:'PKU'};
for(var a in person){
alert(a ": " typeof a);
}

Here, single/double quotes are added to name and age respectively to emphasize that they are of String type, while single/double quotes are not added to school. We traverse the properties of the object to see its type. It is found that school is also implicitly converted to String type.

The index of the array is actually a string type. It's amazing, but it's true. For example,

Copy code The code is as follows:

var ary = [1,3,5,7 ];
for(var a in ary){
alert(a ": " typeof a);
}

3. Exist during alert Implicit type conversion of
Copy code The code is as follows:

String .prototype.fn = function(){return this};
var a = 'hello';
alert(typeof a.fn()); //-->object
alert(a. fn()); //-->hello

Add a fn method to the String prototype, which returns this. We know that this can be understood as an instance object of the current class. Since it is an object, typeof a.fn() naturally returns object.
The key is the last alert(a.fn()). What a.fn() returns is obviously an object, but it is implicitly converted into a string "hello" for display.

The same situation happens with numeric types, such as

Copy code The code is as follows:

Number.prototype.fn = function(){return this};
var a = 10;
alert(typeof a.fn());//-->object
alert(a. fn()); //-->10

a.fn() returns the object type, but it will be converted implicitly when alert(a.fn()) into numbers.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn