1
搬家篇二

javascript数据类型分为基本数据类型和引用数据类型

基本数据:Number, Boolean, Undefined, Null, String;

引用数据:Object;

当0.000...01,小数点后大于等于7位时会自动转换成科学计数法

当20000...00,当整数部分大于等于22位时会自动转换成科学计数法。

Number(x):一个参数;

  • 参数为Number类型时:能正确输出;
console.log(Number(1));        //1
console.log(Number(1e-7));    //1e-7
console.log(Number(0b111));  //7
console.log(Number(NaN));   //NaN
  • 参数为Boolean类型时:true->1;fasle->0;
console.log(Number(true));  //1
console.log(Number(false)); //0
  • 参数为undefined时:其值为NaN
console.log(Number(undefined)); //NaN
  • 参数为null时:
console.log(Number(null));    //0
  • 参数为String类型时:
//数字字符串
console.log(Number("123"));   //123
console.log(Number("-123"));  //-123
console.log(Number("12.3"));  //12.3
console.log(Number("1e-7"));  //1e-7
console.log(Number("0xff"));  //255
console.log(Number("00123")); //123    
console.log(Number("   123")); //123
console.log(Number("\t\n123")); //123
//数字+字符或全字符字符串
console.log(Number('a123'));  //NaN
console.log(Number("false")); //NaN
console.log(Number("a123"));  //NaN
//空串或者空格字符串
console.log(Number("\t\n"));   //0
console.log(Number("  "));     //0
console.log(Number(""));       //0
  • 参数类型是对象时:先进行.valueOf(),如得到的时对象再进行toString(),待得到基本数据类型为止,例如{}.valueOf().toString() = "[object Object]" 最后number的结果就是NaN了
console.log(Number({}));  //NaN
console.log(Number([1])); //1
console.log(Number([1,2]));  //NaN
cosole.log(Number([]));    //0

parseInt(x,y):2个参数,

流程是:先String(x)变为字符串,再将该值以y进制为基底转为十进制数,不填着为10,y的范围: [2,36]

无参数y时:

  • x参数为number时:值得注意⚠️的是0.001,1e-7类型的值,会舍去小数点(e)后面的数值,返回前面的。
console.log(parseInt(123));   //123
console.log(parseInt(1e-7));  //1
console.log(parseInt(0xff));  //255
console.log(parseInt(NaN));   //NaN
console.log(parseInt(0.00001)); //0
  • x参数为boolean,undefined,null时:
console.log(parseInt(true));  //NaN
console.log(parseInt(false)); //NaN
console.log(parseInt(undefined)); //NaN
console.log(parseInt(null));    //NaN
  • x参数为String类型时:需要注意的时空串,空格字符串,数字+字符

console.log(parseInt(""));   //NaN
console.log(parseInt("-123"));  //-123
console.log(parseInt("  "));  //NaN
console.log(parseInt("\t\n"));   //NaN
console.log(parseInt("a123")); //NaN
console.log(parseInt("123a")); //123
  • 参数x为对象时:同理通过.valueOf(),如得到的时对象再进行toString(),待得到基本数据类型为止,然后按上述规则进行输出
console.log(parseInt({1:2}));   //"[object Object]"->NaN
console.log(parseInt([]));      //""->NaN
console.log(parseInt([1,2]));   //"1,2"->//1
  • 带参数x,y,当y为0,null,undefined,NaN时,会将y忽略默认为10,超过[2,36]的范围时返回NaN,当x值无妨被y进制表示时返回NaN,其余能表示几位就几位
console.log(parseInt("f",2));   //NaN
console.log(parseInt("11f",2);  //3
console.log(parseInt("123",37)); //NaN
console.log(parseInt("0xff",0));  //255
console.log(parseInt("0xff",NaN)); //255
console.log(parseInt("ff",[]));  //NaN

parseFloat(x):一个参数,能正确表示浮点数以及科学计数法,但是对字符串进制数不能正确表示,其余与parseInt(x,10)一样

console.log(parseFloat(0xff));  //255
console.log(parseFloat("0xff"));  //0  
console.log(parseInt("0xff"));   //255  个人猜测parseInt(x,y)有y的存在所以能正确输出
console.log(parseFloat(1e22));   //1e22
console.log(parseFloat(1e-7));   //1e-7
console.log(parseFloat("00123"));  //123
console.log(parseFloat(" "));     //NaN

String(), .toString()

  • 第一:undefined, null没有toString()属性只能通过String(undefiend)
  • 第二:y.toString(x), 的意思是将目标数值y转为x进制的数值
console.log(String(null));  //"null"
console.log(0xff.toString(2));  //"11111111"
console.log(oxff.toString()); //"255" 不填默认10进制

Infinity
293 声望9 粉丝

学前端中,只想激情优雅的写代码