javascript -- 数据类型

一、数据类型

  1. 基本数据类型

    String字符串
    number数字
    boolean布尔值
    null       空对象(尚未存在的函数)
    undefined未赋值(没有赋值的变量)
    symbol符号类型
    Biglnt大整数类型
  2. 引用数据类型

    funciton函数
    array数组
    object对象
  3. es6中新增的类型

    object类的细分
    Set集合,允许你存储任何类型的唯一值,无论是原始值或者是对象引用。
    Map对象保存键值对,并且能够记住键的原始插入顺序。任何值
    WeakSet类似Set,但不能使用entries
    WeakMap类似Map,但是不能使用entries
    TypedArray定型数组

二、数据类型的检测

  1. typeof
            适用于检测基本数据类型、function(假设检测数组与对象,返回结果都是object,无法判断是哪个引用类型)

    // typeof 检测简单类型
    
    let type_undefind = undefined;
    //输出结果 :undefined
    console.log(`typeof undefind------ ${typeof type_undefind}`); 
    
    let type_symbol = Symbol("test");
    //输出结果 :symbol
    console.log(`数据类型------ ${typeof type_symbol}`);
    
  2. instanceof
          用于检测数据属于哪一个构造函数,此方法要求明确地确认对象为某一类特定型,(可以检测数组类型,但是还是无法分辨Object,基本类型也无法判断

     1.判断一个实例是否属于某种类型

    //1.判断一个实例是否属于某种类型
        var arr =  [];
        console.log( arr instanceof Array);

    2..在继承关系中,用来判断一个实例是否数据他的父类型

    //2.在继承关系中,用来判断一个实例是否数据他的父类型
        function Aoo(){}
        function Boo(){}
        Boo.prototype = new Aoo();
        var boo = new Boo();
        console.log(boo instanceof Boo);    //true
        console.log(boo instanceof Aoo);    //true

  3. Object.prototype.toString.call() 
    使用以上方法可以很好的检测数据类型,但是无法区分自定义对象类型,自定义对象类型采用instanceof 区分

    var fun = function(){};
    var obj = Object.prototype.toString.call(fun);
    console.log(obj); //[object Function]
    console.log(obj === "[object Function]"); //true
  4. isNaN
      判断是否为NaN,NAN也是number类型,使用Number是检测不出来的,需要使用isNAN()的方式

    let str = NaN ;
    console.log(isNaN(str)); //true
    console.log(typeof str);    //number
    
    

三、数据类型的转换

     强制类型转换

String()转字符串类型
toString()转字符串类型(布尔值与数字类型皆可,null 与undefined不可)
Number()转数字类型(整型)
Boolean()转布尔值
parseInt()转整型
parseFloat()转浮点数


       隐式类型转换

        1.任意数据类型 + 字符串 ,均换成字符串类型

let bool = false ;

let str = "aa";

let strBool = str + bool;

console.log(typeof strBool); //String

        2.数字类型的字符串 - 数字 ,均转成数字 (一般情况都 -0)

let str = "123";
console.log(typeof (str -0) ); //number

        3.不是数字类型的字符串 - 数字 ,均为NaN

let str = "123aa";
console.log(typeof (str -0) ); //NaN
console.log(isNaN(str - 0)); //true;

        4.false == “ ”  == 0  、 null == undefined 、true == 1

console.log(null == undefined); //true
console.log(false == 0);    //true
console.log(false == "");   //ture
console.log(0 == "");    //ture
console.log(true == 1);  //ture

        5.NaN (Not  a Number ) 是一个特殊的数字,NaN != NaN 

console.log(NaN == NaN); //false

四、总结

        在日常的开发中经常需要判断数据的类型,处理后端返回的数据进行数据的转换。学习数据类型还是很有必要的,还有一些 json格式的转换什么的,时间的转换啥的,项目大部分都是数据处理跟渲染。还是很有必要学好数据类型的知识。

简易版----封装一个判断数据类型的函数(类)

class ValidType {
    constructor() {

    }
    isType = (o) => {
        return Object.prototype.toString.call(o).slice(8, -1);
    }
    //判断是否为 数值
    isNumber = (v) => {
        return this.isType(v) === 'Number';
    }
    //判断是否为 布尔值
    isBoolean = (v) => {
        return this.isType(v) === 'Boolean';
    }
    //判断是否为 字符串
    isString = (v) => {
        return this.isType(v) === 'String';
    }
    //判断是否为 数组
    isArray = (v) => {
        return this.isType(v) === 'Array';
    }
    //判断是否为 对象
    isObject = (v) => {
        return this.isType(v) === 'Object';
    }
    //判断是否为 函数
    isFunction = (v) => {
        return this.isType(v) === 'Function';
    }
    //判断是否为 日期对象
    isDate = (v) => {
        return this.isType(v) === 'Date';
    }
    //判断是否为 集合
    isSet = (v) => {
        return this.isType(v) === 'Set';
    }
    //判断是否为 正则表达式
    isRegExp = (v) => {
        return this.isType(v) === 'RegExp';
    }
    //判断是否为 Error
    isError = (v) => {
        return this.isType(v) === 'Error';
    }
    //判断是否为 Symbol
    isSymbol = (v) => {
        return this.isType(v) === 'Symbol';
    }
    //判断是否为 promise
    isPromise = (v) => {
        return this.isType(v) === 'Promise';
    }
    //判断是否为 undefined
    isUndefined = (v) => {
        return this.isType(v) === 'Undefined';
    }
    //判断是否为 空值
    isNull = (v) => {
        return this.isType(v) === "Null";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值