I can see three variables that declare a value type. I smile at the corner of my mouth: You are still young, and there are at least five variables I know. If you are curious, you can refer to them, I hope this article will help you chat with a group of 90 young people who claimed to be Xiao Fang. IT male's bad habit is to talk about the technology and then talk about the technology. Xiao Fang suddenly asked
1. I can see three variables that declare a numerical value. What are the differences:
The Code is as follows:
Var num = 123; // method 1
Var num = Number (123 );
Var num = newnumber (123 );
2. method 1 is clearly a digital literal. For Mao, we can directly call various methods on the above, as shown below:
The Code is as follows:
Var num = 123;
Console. log (num. toString ());
I smiled at the corners of my mouth: you are still young. What are the three types? I know at least five types !!
With a smile, the mouth of the mouth began to twenty, and the forehead began to sweat: at least five kinds, yes,... What is the difference...
With the tolerance and pride of the old cainiao, I disdainfully said: I don't know. I will check the information myself... Turn around and start turning over ECMAS-262 (fifth edition)
I. Five Ways to declare numeric Variables
The Code is as follows:
// Method 1: The most common method is to declare data using numbers literally.
Var num = 123;
// Method 2: occasionally used. In most cases, the string is converted into a number.
Var num = Number (123 );
// Method 3: rarely used. Various books, including the rhinoceros, are not recommended.
Var num = newnumber (123 );
// Method 4: God mode, which has not been used yet
Var num = new Object (123 );
// Method 5: more bizarre and more strange
Var num = Object (123 );
As you can see, there are five declaration methods. method 1 is usually used like this. Method 3 to Method 5 is used for comparison. The following sections describe the following:
1. What are the differences between the five declaration methods? What happened when you knocked on the Code with your trembling fingers?
2. the Declaration in method 1 is clearly not an object, but why can we call methods such as toString on it?
Ii. Differences between various declarations
Mode 1: var num = 123;
EC5 description:
A numeric literal stands for a value of the Number type. this value is determined in two steps: first, a mathematical value (MV) is derived from the literal; second, this mathematical value is rounded as described below
// Summary:
1. parse the value of the variable, such as taking out the integer part and fractional part, because the numerical declaration method can also be num =. 123, num = 123e4, etc.
2. Take an approximate value for the parsed value, such as num = 123. 33333333333333... 3333333333333333333333333.... At this time, take the approximate value. If the approximate value is used, the rule is not expanded.
3. The variable declared in this way is a simple numeric literal, not an object (as to why toString and other methods can be called above, I will explain later)
Method 2: var num = Number (123);
EC5 description:
15.7.1 The Number Constructor Called as a Function
When Number is called as a function rather than as a constructor, it performs a type conversion. 15.7.1.1 Number ([value])
Returns a Number value (not a Number object) computed by ToNumber (value) if value was supplied, else returns + 0. Summary:
1. Here we only call Number as a common function, rather than a constructor. Therefore, the returned value is not an object, but a simple value.
2. the essence is the same as method 1. The difference from method 1 is that you need to execute different type conversion procedures for the type of input parameters and try to parse the parameters into corresponding values. The specific rules are as follows:
Method 3: var num = new Number (123);
EC5 description:
15.7.2 The Number Constructor
When Number is called as part of a new expression it is a constructor: it initialises the newly created object. 15.7.2.1 new Number ([value])
The [[Prototype] internal property of the newly constructed object is set to the original Number prototype object, the one that is the initial value of Number. prototype (15.7.3.1 ).
The [[Class] internal property of the newly constructed object is set to "Number ".
The [[PrimitiveValue] internal property of the newly constructed object is set to ToNumber (value) if value was
Supplied, else to + 0.
The [[Extensible] internal property of the newly constructed object is set to true. Summary:
1. Here we call the Number constructor method, and return a Number type object. This object can access the prototype attributes and methods of the Number. This may be confusing and will be discussed later.
The Code is as follows:
Var num = newnumber (123 );
Console. log (typeof num); // output: object
Console. log (Object. prototype. toString. call (num); // output: [object Number]
3. the original value of the returned Number type object ([[PrimitiveValue]) is the numeric value obtained after type conversion. The specific conversion rules are the same as those mentioned in method 2.
Method 4: var num = new Object (123 );
EC5 description:
15.2.2 The Object Constructor
When Object is called as part of a new expression, it is a constructor that may create an object.
15.2.2.1 new Object ([value])
When the Object constructor is called with no arguments or with one argument value, the following steps are taken:
1. If value is supplied, then
A. If Type (value) is Object, then
1. If the value is a native ECMAScript object, do not create a new object but simply return value.
2. If the value is a host object, then actions are taken and a result is returned in an implementation-dependent manner that may depend on the host object.
B. If Type (value) is String, return ToObject (value ).
C. If Type (value) is Boolean, return ToObject (value ).
D. If Type (value) is Number, return ToObject (value ).
2. Assert: The argument value was not supplied or its type was Null or Undefined.
3. Let obj be a newly created native ECMAScript object.
4. Set the [[Prototype] internal property of obj to the standard built-in Object prototype object (15.2.4 ).
5. Set the [[Class] internal property of obj to "Object ".
6. Set the [[Extensible] internal property of obj to true.
7. Set all the internal methods of obj as specified in 8.12.
8. Return obj.
Personal Understanding:
A lot of text is posted on the top, which may be a headache, because variables are declared using the new Object (param) method, depending on the type of the input parameter, the results are also different, such as the data type. Here, the Mask Body conversion rules are negligible. We only look at the section of our current relationship, that is, the text marked red above. The main points are:
1. If a parameter is passed and the parameter is a Number, an object of the Number type is created and returned.
2. The value of this Number object is equal to the passed parameter. The internal [prototype] attribute points to Number. prototype.
Method 5: var num = Object (123 );
EC5 description:
15.2.1 The Object Constructor Called as a Function
When Object is called as a function rather than as a constructor, it performs a type conversion.
15.2.1.1 Object ([value])
When the Object function is called with no arguments or with one argument value, the following steps are taken:
1. If value is null, undefined or not supplied, create and return a new Object object exactly as if the standard built-in Object constructor had been called with the same arguments (15.2.2.1 ).
2. Return ToObject (value ).
Personal Understanding:
1. When the input parameter is null, undefined, or null, it is equivalent to new Object (param), And param is the user-passed parameter.
2. Otherwise, an object is returned. For the type of the object to be converted, see the following table. The example above is essentially equivalent to new Number (123 ):
3. Simple Test Cases
The Code is as follows:
Var num = Object (123 );
Console. log (typeof num); // output: object console. log (Object. prototype. toString. call (num); // output: [object Number]
Iii. var num = 123; and var num = new Number (123);
Xianxianzhe warned us that it is best to use the first method. The reasons are as follows: low efficiency, and unexpected events may occur when eval (num) occurs... Balabara
Aside from the above noises, we should pay attention to the following reasons:
The Code is as follows:
Var num = 123;
Console. log (num. toString (num); // output: '000000'. No error occurred.
Doesn't it mean that the literal method declares only common numeric types, not objects? It is not scientific to call the toString method of the object!
Okay, I checked the rhino book and found the answer.:
When a user declares a variable literally and calls methods such as toString on the variable, the JS Script Engine secretly creates the packaging object corresponding to the variable, and call the corresponding method on the object. When the call ends, the object is destroyed. This process is invisible to users, so many beginners may be confused about this.
Well, I admit that the above paragraph is not the original article, but my personal understanding of the corresponding section of the rhino book, In order to seem more professional and authoritative, I intentionally added a reference mark... The example above can be simply considered as the following process:
The Code is as follows:
Var num = 123;
Var tmp = num;
Num = new Number (num );
Console. log (num. toString (num ));
Num = tmp;
(Because the specification was turned over to 1 point last night, it was really difficult, so I was lazy. I believe the rhino book won't hurt me)
4. Post
Javascript variable declaration methods, type judgment, etc., have always been unable to speak out, the above content for beginners, is tantamount to destroying three views; even old cainiao like myself who have been with Javascript for more than two years are often confused
Summary:
1. method 1 and method 2 are essentially the same
2. Method 3, Method 4, and Method 5 are essentially the same
Last:
If there are any mistakes or omissions in this example, please point out. If you think the article is useful to you, click "recommended ":)