JS中的数据类型-web前端高级学习

JS中的数据类型 7+2
 - 基本数据类型(也称值类型):
  number、 string、boolean、null、undefined、 symbol、bigint===>ES6中新增了两种数据类型symbol和bigint

 - 引用数据类型:对象(object) 、函数(function)

  对象包含:普通对象、数组对象、正则对象、日期对象、Math数学函数对象...

数据类型检测

   - typeof 检测数据类型的逻辑运算符
   - instanceof 检测是否为某个类的实例
   - constructor 检测构造函数
   - Object.prototype.toString.call 检测数据类型的

typeof [value]  返回当前值的数据类型  ---"数据类型"==>

字符串“number”、字符串“boolean”、字符串“undefined”、字符串"string"、字符串""symbol"、字符串""function"、字符串“object”、字符串“bigint”
  - 返回的结果都是字符串
  - 局限性:
    + typeof (null)=> "object"
    + typeof 不能细分对象类型(检测普通对象或者数组对象等都是"object")

把其它数据类型转换为数字的方法
  + 强转换(基于底层机制转换的) Number([value])
    + 一些隐式转换都是基于Number完成的
      + isNaN('12px') 先把其它类型值转换为数字在检测
      + 数学运算 '12px'-13
      + 字符串==数字 两个等于号比较很多时候也是要把其它值转换为数字
      + ...
  + 弱转换(基于一些额外的方法转换) parseInt([value])/parseFloat([value])

-------------------------------------------------------------------------------------------------------------------

习题:

 let a = typeof typeof typeof [12, 23];
 console.log(a); //=>"string"   
    /*
    * typeof [12, 23] =>"object"
    * typeof "object" =>"string"
    * ...
    */

  

1、 NaN!=NaN  它和谁都不相等,包括和自己本身也不相等

2、isNaN(值)  检测这个值是否为有效数字,如果不是有效数字返回TRUE,是有效数字返回FALSE

3、 parseInt 处理的值是字符串,从字符串的左侧开始查找有效数字字符(遇到非有效数字字符则停止查找)

  -> 如果处理的值不是字符串,需要先转换为字符串然后在开始查找接口


4、Number (值) 直接调用浏览器最底层的数据类型检测机制来完成
   + true 1   false 0  ===============   Number (true)=>1、Number (false )=>0
   + null 0    undefined NaN============= Number (null)=>0、Number (NaN)=>NaN
   + 字符串中必须保证都是有效数字才会转换为数字,否则都是NaN==========Number (“字符串”)=>NaN

  +Number("")=>0  小技巧:遇到Number(值)值是非字符串的想象放到if(值)

5、0 NaN null undefined  转为布尔类型是假   一般借用感叹号!转成布尔类型

    let res = parseFloat('left:200px'); //=>NaN
    if (res === 200) {
	alert(200);
    } else if (res === NaN) { //NaN!=NaN
	alert(NaN);
    } else if (typeof res === 'number') { //=>typeof NaN => "number"
	alert('number');
   } else {
	alert('Invalid Number');
   } 

  

 parseInt("")         //NaN
 Number("")         //0
 isNaN("")            //先把""转换为数字(隐式 Number)  isNaN(0)  false
 parseInt(null)       //parseInt('null')  NaN 
Number(null)       //0 isNaN(null)           // isNaN(0) false parseInt("12px")       //12 Number("12px")         //NaN isNaN("12px")         //isNaN(NaN) true parseFloat("1.6px") + parseInt("1.2px") + typeof parseInt( null);    //1.6 + 1 + typeof NaN => 2.6 + 'number' -> '2.6number' isNaN(Number(!!Number(parseInt("0.8"))));   // isNaN(0) false typeof !parseInt(null) + !isNaN(null);   // 'booleantrue'

 

 []==true 都转换为数字    ====>  ==号比较两边利用Number转换机制
 Number([]) Number('') 0  =======>想象if([])  

let result = 10 + false + undefined + [] + 'Tencent' + null + true + {};
 console.log(result);

	// 10 + 0  10
	// 10 + undefined  NaN
	// NaN + []  'NaN'
	// 'NaN' + 'Tencent'   'NaNTencent'
	// 'NaNTencentnulltrue[object Object]'

+号

 本身是运算符,在没有遇到字符串或引用类型值之前,会把各个值用Number机制转换成数字类型相加

当遇到字符串就与字符串拼接,遇到引用类型如 [] 使用(值).toString()方法转换成字符串拼接

NaN+[]==>"NaN"     NaN+[1,3]===>"NaN1,3"

NaN+{}==>"NaN[object Object]"

{}+10==>10    {}在前会被当成代码块执行,  10+{}===>"10[object Object]"

 

原文地址:https://www.cnblogs.com/zhouyuxiang/p/12753359.html