An analysis of implicit type conversion _javascript techniques in JavaScript

Source: Internet
Author: User
Tags arithmetic string to number

If you use a function or method call to explicitly convert a type to another type, called a display transformation, it is called an implicit type conversion. Google and Wikipedia did not find the word "show type conversion", "Implicit type conversion". Call it that.

Implicit type conversion in operation

1, "+" operator

Copy Code code as follows:

var a = one, B = ' 22 ';
var C = a + B;

Here the engine will first turn a into a string "11" and then connect to B, and become "1122". Some people will have a question, why not turn B into number 22 and then arithmetic addition, so that C is 33. No why, when the operator "+" on either side is a numeric type, and one is a string type, the JS engine prescribes a string concatenation operation rather than an arithmetic addition. Using the operator "+" feature, it is convenient to convert number to string. Such as
Copy Code code as follows:

var a = 11;
Alert (typeof a); -->number
A = a + ';
Alert (typeof a); -->string

2, "-" operator

"-" can be a unary operator (negative), or it can be a two-dollar (subtraction). Such as

Copy Code code as follows:

var a = one, B = ' 5 ';
var c = a-b;
Alert (typeof c); --> number

In contrast to the above "+", the string B is implicitly converted to the number 5 for arithmetic subtraction. With this feature, you can easily convert a string to number
Copy Code code as follows:

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

implicit type conversions in statements

1,if

Copy Code code as follows:

var obj = {name: ' Jack '}
if (obj) {
Todo
}

This will implicitly convert obj to a Boolean type

2,while

Copy Code code as follows:

var obj = {name: ' Jack '}
while (obj) {
Todo
}

Same if

3,for in-Time type conversions
An implicit conversion from an identifier to a string occurs when an object literal is defined.

Copy Code code as follows:

var person = {' name ': ' Jack ', ' age ': 20,school: ' PKU '};
For (var A on person) {
Alert (A + ":" + typeof a);
}

Here Name,age the single/double quotes to emphasize the string type, and the school does not add single/double quotes. We iterate over the object's properties to see its type. The discovery of school was also implicitly converted to a string type.

The index of an array is actually a string type. It's amazing, but it's true. Such as

Copy Code code as follows:

var ary = [1,3,5,7];
For (var A in ary) {
Alert (A + ":" + typeof a);
}

Implicit type conversion at alert
Copy Code code as follows:

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

Adds a FN method to the string stereotype, which returns this, and we know that this can be understood as an instance object of the current class, and since it is the object then typeof A.fn () natural return is object.
The key is the last alert (A.fn ()), A.fn () returns clearly the object, but implicitly converts it to the string "Hello" display.

The same situation occurs on the number type, such as

Copy Code code 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 implicitly converts it to a number when alert (A.fn ()).

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.