With the group of a self-proclaimed small side of the sale of 90 young boys chat, it men's bad habits, chatting chatting to the technology up, Fangfangfang suddenly asked
1. Declare a variable of a numeric type I see three kinds of different:
Copy Code code as follows:
var num = 123; Way One
var num = number (123);
var num = new number (123);
2, the way a clearly is a number of literal, for Mao ordinary we can directly in the above invoke various methods, as follows:
Copy Code code as follows:
var num = 123;
Console.log (Num.tostring ());
My mouth slightly a smile: juvenile you still tender point, which stop three kinds, I know at least five kinds!!
Smiled and smiled at the mouth began to twitch, forehead began to sweat: at least five species, yes, but ... What's the difference?
With the special Reserve and pride of the old rookie, I disdain to say: "This does not know, oneself check information to go ..." Turn around and start flipping through the ECMAS-262 (fifth edition)
one or five ways to declare a numeric type variable
Copy Code code as follows:
Way one: The most common way to declare by means of numeric literal
var num = 123;
Mode two: The occasional use of the way, most of the time is the string into a number
var num = number (123);
Mode III: Rarely used, the various gods books, including Rhino books, are included in the Not recommended way
var num = new number (123);
Mode four: God way, has not yet seen the use of people
var num = new Object (123);
Way five: More bizarre, more bizarre
var num = Object (123);
You can see that in the last 5 kinds of declaration way, the way one does not have to say, is usually used in this way; mode three to mode five is used for comparison, the following will be explained separately:
1. What is the difference between five types of declaration methods? When you hit the code with a shaky finger, what happened to God's horse?
2. The way one statement is clearly not an object, but why can we normally invoke methods such as ToString?
ii. differences between various forms of declaration
Mode one: var num = 123;
EC5 Description:
A numeric literal stands for a value of the number type. This is determined in two Steps:first, a mathematical value (MV) are derived from the literal; Second, this mathematical the value is rounded as described below
//..... Summary of Personal Summary:
1. Parse the value of the variable, for example, take out the integer part, the decimal part, and so on, because the digital declaration way can also be num =. 123,num = 123e4, etc.
2. Approximate value of the parsed value, such as num = 123.33333333333333...3333333333333333333333333 ..., this time to take the approximate value, the specific approximation of the rule does not expand
3. The variable declared in this way is just a simple number literal, not an object (as for why you can invoke ToString on the above, after the text)
mode two: var num = number (123);
EC5 Description:
15.7.1 the number constructor called as a Function
When the 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 is supplied, else Returns +0. Summary of Personal Summary:
1. This is just called number as a normal function instead of constructing the method, so the return is not an object, but a simple numerical
2. The essence is the same as the way; the difference is that, for the type of incoming parameter, different type conversion procedures are required to parse the parameters into the corresponding values, as follows:
mode three: 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 are set to the original number Prototype object, the E 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 are set to Tonumber (value) if value was
Supplied, else to +0.
the [[[extensible]] internal property of the newly constructed object are set to true. Summary of personal Summary:
1. Here the number action constructs a method call, which returns an object of type number that has access to the prototype properties and the methods of the few; that might be confusing
Copy Code code as follows:
var num = new number (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]]), which is the same as the numeric value obtained after the type conversion, and the specific conversion rule and mode two refer to
mode four: var num = new Object (123);
EC5 Description:
15.2.2 the Object constructor
When the Object is called as part of the A new expression, it is a constructor this may create Object.
15.2.2.1 new Object ([value])
When the Object constructor was called with no arguments or with one argument value, the following steps are:
1.If value is supplied, then
A. If Type (value) is Object, then
1.If The value is a native ECMAScript object, does not create a new object but simply return value.
2.If The value is a host object, then actions are taken and a are returned in a implementation-dependent tha T may depend on the host object.
B. If Type (value) is String, return Toobject (value).
C. If Type (value) is a Boolean, return Toobject (value).
D. If Type (value) is number, return Toobject (value).
2.assert:the argument value is not supplied or it 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 the internal methods of obj as specified in 8.12.
8.Return obj.
explanation of personal understanding:
There are a lot of words on the above, may look a bit headache, because through the new Object (param) This way to declare variables, depending on the type of incoming parameter specific types, the results will be different, such as data types. This specific conversion of the rules, can be ignored, we only look at the current part of our relationship, that is, the above marked red text, the main points are:
1. Pass the parameter, and the parameter is a number, then create and return an object of type numbers-yes, in fact, equivalent to mode three
2. The value of the number object is equal to the passed parameter, and the internal [[prototype]] property points to the Number.prototype
Mode five: var num = Object (123);
EC5 Description:
15.2.1 the Object constructor called as a Function
When the 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:
1.If value is null, undefined or isn't supplied, create and return a new object exactly as If the standard built-in O Bject constructor had been called with the same arguments (15.2.2.1).
2.Return Toobject (value).
explanation of personal understanding:
1. When the passed parameter is null, undefined, or null, it is equal to the new Object (param), param the parameter passed to the user
2. Otherwise, return an object, as to the specific type of object, see the table below; The above example is essentially equivalent to new number (123):
3. Simple test Case
Copy Code code as follows:
var num = Object (123);
Console.log (typeof num); Output: Object Console.log (Object.prototype.toString.call (num)); Output: [Object number]
three, var num = 123; with var num = new Number (123);
First of all, the sages warned us to use the first way, the reason ingredient, the resounding: low efficiency, eval (num) when there may be unexpected circumstances occur ... Balabala
Put aside the noise above, we are here to focus on why the following statement does not go wrong:
Copy Code code as follows:
var num = 123;
Console.log (num.tostring (num)); Output: ' 123 ', no mistake
is not that the literal way of declaring is just a normal numeric type, not an object? But not the object where the ToString method call, this unscientific!
All right, check the Rhino book and find the answer.:
When a user declares a variable by literal means, and on the variable, such as ToString method, the JS script engine secretly creates the corresponding wrapper object for the variable, and invokes the corresponding method on the object, and destroys the object when the call is finished; The process is not visible to the user, So many beginners will have this confusion.
Well, I admit that this is not the original text, but a personal understanding of the corresponding paragraphs of rhino book, in order to appear more professional authority deliberately added a reference logo ... The example above can be considered simply as follows:
Copy Code code as follows:
var num = 123;
var tmp = num;
num = new number (num);
Console.log (num.tostring (num));
num = tmp;
(Because last night turned over the specification turned 1, really sleepy not, lazy, I believe Rhino book will not pit me)
four, write in the back
JavaScript variable declaration way, type judgment, etc., has always felt powerless to spit, the content of the above for beginners, is tantamount to destroying the three views; even for the old rookie, who has been with JavaScript for more than two years, is often confused.
Briefly summarize:
1. Mode one, way two essentially the same
2. Mode three, the way four, the way five essentially the same
Last and Last:
For example, there are errors, please point out, if you think the article is useful to you, you can click on the "recommended":)