跟小组里一自称小方方的卖萌90小青年聊天,IT男的坏习惯,聊着聊着就扯到技术上去了,小方方突然问
1、声明一个数值类型的变量我看到三种,区别在哪:
复制代码 代码如下:
var num = 123; //方式一
var num = Number(123);
var num = new Number(123);
2、方式一明明是个数字字面量,为毛平常我们可以直接在上面调用各种方法,如下:
复制代码 代码如下:
var num = 123;
console.log(num.toString());
我嘴角微微一笑:少年你还嫩了点,哪止三种,我知道的至少有五种!!
笑着笑着嘴角开始抽搐,额角开始冒出了冷汗:至少有五种,没错,但是。。。区别在哪。。。
怀着老菜鸟特有的矜持和骄傲,我不屑地说:这都不知道,自己查资料去。。。转过身,开始翻ECMAS - 262(第五版)
一、五种声明数值类型变量的方式
复制代码 代码如下:
//方式一:最常见的方式,通过数字字面量方式声明
var num = 123;
//方式二:偶尔使用方式,大部分情况下是将字符串转成数字
var num = Number(123);
//方式三:很少使用,各神书,包括犀牛书,都将其列入不推荐方式
var num = new Number(123);
//方式四:神方式,目前还没见过人使用
var num = new Object(123);
//方式五:更离奇,更诡异
var num = Object(123);
可以看到,在上5种声明方式种,方式一不用怎么说了,平常都是这样用的;方式三 to 方式五属于比较的使用,下文会分别说明:
1.五种声明方式的区别?当你用颤巍巍的手指敲下代码后,究竟发生了神马?
2.方式一声明的明明不是对象,但为什么平常我们可以在上面调用方法,如toString等?
二、各种声明方式之间的区别
方式一:var num = 123;
EC5说明:
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
//.....个人总结摘要:
1.解析变量的值,比如说取出整数部分、小数部分等,因为数字声明方式还可以为num = .123,num = 123e4等形式
2.对解析出来的值取近似值,比如num = 123.33333333333333...3333333333333333333333333....,这个时候就要取近似值了,具体取近似则规则不展开
3.此种方式声明的变量,只是个简单的数字字面量,并不是对象(至于为什么可以在上面调用toString等方法,后文讲解)
方式二:var num = Number(123);
EC5说明:
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.个人总结摘要:
1.此处只是将Number当作一个普通的函数来调用,而不是构造方法,因此返回的不是对象,而是一个简单的数值
2.本质与方式一相同;相对于方式一的区别在于,需要针对传入参数的类型,执行不同的类型转换过程,试图将参数解析成对应的数值,具体规则如下:
方式三:var num = new Number(123);
EC5说明:
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.个人总结摘要:
1.此处将Number作用构造方法调用,返回的是Number类型的对象,该对象能够访问Number的原型属性以及方法;这样说可能有些迷惑,后面会说到
复制代码 代码如下:
var num = new Number(123);
console.log(typeof num); //输出:object
console.log(Object.prototype.toString.call(num)); //输出:[object Number]
3.返回的Number类型对象内部的原始值( [[PrimitiveValue]]),为经过类型转换后获得的数字值,具体转换规则与方式二提到的一致
方式四:var num = new Object(123);
EC5说明:
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.
个人理解说明:
上面洋洋洒洒的贴了好多文字,可能看着有些头疼,因为通过 new Object(param) 这种方式声明变量,根据传入参数具体类型的不同,得到的结果也会有区别,比如说数据类型。这里面具体转换的规则,可以忽略不计,我们只看我们目前关系的部分,即上面标红色的文字,要点有:
1.传递了参数,且参数是一个数字,则创建并返回一个Number类型的对象 —— 没错,其实等同于方式三
2.该Number对象的值等于传入的参数,内部的[[prototype]]属性指向Number.prototype
方式五:var num = Object(123);
EC5说明:
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).
个人理解说明:
1.当传入的参数为空、undefined或null时,等同于 new Object(param),param为用户传入的参数
2.否则,返回一个对象,至于具体转换成的对象类型,可参见下表;具体到上面的例子,本质等同于new Number(123):
3. 简单测试用例
复制代码 代码如下: