JavaScript is the magician---automatic type conversion

Source: Internet
Author: User

Before talking about this problem, it is necessary to mention the five basic types of JS, there are two kinds of online, six, seven, eight kinds of. This is not important, it is important that you understand and you practice are consistent on it. Do not read books as much as you believe, so practice more. According to the results returned by TypeOf, the five categories commonly used are as follows:

    • Undefined-If the variable is of type undefined
    • Boolean-If the variable is of type Boolean
    • Number-If the variable is of type number
    • String-If the variable is of type string
    • Object-If the variable is a reference type or a Null type

Let's look at a simple code:

var str = ' Hello world ';

Plain no odd code, obstinately someone raised a question. This str is clearly a basic string type, how to say a String object on the world-class?

There is a picture of the truth. I don't mean to have a black-out. But I don't think that's what the string object is. So the question is, why not an object, and how do you invoke those methods on the string object?

This leads to the "implicit conversion of types" issue. Before discussing this issue further, let's look at a more common example:

var num = ' 3 '; var res = ten-//  7; typeof // string;

Num is a character 3, according to the common sense is not allowed to participate in the calculation, but I hard to take it and 10 to do subtraction, the results actually give a normal situation, only the number 3 will have the results. It's amazing, isn't it? Let me simulate this auto-implicit conversion:

var num = ' 3 '; var temp = number (num); var res = ten-null; num;

But a lot of people don't have any questions about it, take it for granted, but the same thing happens when they're on a string, they get confused. I'll take a good look at this "implicit automatic type conversion" problem. Let's look at the following code:

var str = ' Hello '; Str.charat (//e

When we raise the Charat method in str, JS will automatically perform the following conversions in the background:

var temp = new Object (str);

Output Temp.charat (1);

temp = null;

When the iron is hot, let's look at one more example:

var str = ' Hello '= ' frog ';//No error Console.log (str.name);//undefined

STR is a string, but it can add properties like an object, but it clearly sets a property, but it does not come out. Let's try it again using the principle above:

var temp = new Object (str);

Temp.name = ' frog ';

temp = null;

We add the name attribute to STR, which occurs on the intermediate object of temp, so there is no error and is completely legal. But this temp is not own after all, will also be finished. When we call on STR just set the

Name property, the above process is triggered again:

var temp = new Object (str);

Find temp.name for undefined;

temp = null;

So nothing can be found, but there is no mistake. This is why you can add properties, but not out of the reason, because we are operating in an intermediate state, the system will be the case for us to automatically perform this conversion process.

JavaScript is the magician---automatic type conversion

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.