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