数据类型深入理解

数据类型分类

基本(值)类型(5种)

  • String:任意字符串
  • Number:任意的数字
  • booleantrue/false
  • nullnull
  • undefinedundefined

对象(引用)类型(3种)

  • Object:任意对象
  • Array:一种特别的对象(数值下标,内部数据是有序的)
  • Function:一种特别的对象(可以执行)

数据类型判断(3种方式)

typeof :返回数据类型的字符串表达

var aconsole.log(a) // undefinedconsole.log(typeof a) // "undefined"console.log(a === undefined) // trueconsole.log(typeof a === undefined) // falseconsole.log(typeof a === "undefined") // trueconsole.log(undefined === "undefined") // falsea = 4console.log(typeof a) // "number"console.log(typeof a === Number) // falseconsole.log(typeof a === "number") // truea = "hahha"console.log(typeof a) // "string"a = falseconsole.log(typeof a) // "boolean"a = nullconsole.log(typeof a) // objectconsole.log(a === null) // true

注意:typeof返回的是数据类型的字符串表达形式。

typeof true //"boolean"typeof "hahha" //"string"typeof 12 //"number"typeof null //"object"typeof ccc //"undefined"typeof function(){} //"function"typeof {} //"object"

instanceof:类型的实例

首先要理解instanceof的含义:
  • instance例子的意思,A instanceof B 实际上是判断A是否是B的一个实例。理解了这一点,就不难判断类型了。
var b1 = {    b2: [1, "hehe", console.log],    b3: function () {        console.log("b3")        return function  () {            return "Mandy"        }    }}console.log(b1 instanceof Object) // trueconsole.log(b1.b2 instanceof Array, b1.b2 instanceof Object) // true trueconsole.log(b1.b3 instanceof Function, b1.b3 instanceof Object) //true trueconsole.log(typeof b1.b2) // "object"console.log(typeof b1.b3) // "function"console.log(typeof b1.b2[1]) // "string"console.log(typeof b1.b2[2]) // "function"b1.b2[2](555) // 555console.log(b1.b3()()) // "b3" "Mandy"

注意:

  • 函数既是 Function 类型,也是 Object 类型
  • 数组既是 Array 类型,也是 Object 类型

===

  • 可以判断undefined null
ccc === "undefined" // truenull === null // true

总结

  • typeof :

    • 可以判断 undefined / 数值 / 字符串 / 布尔值 / function
    • 不能判断 nullobject, arrayobject

      typeof null // "object"typeof [] // "object"
  • instanceof:

    • 判断对象的具体类型
    • A instanceof B
  • ===:

    • 可以判断 undefined , null

undefinednull 的区别?

  • undefined 代表定义了,未赋值
  • null 代表定义了,并且赋值了,只是赋的值为 null
// undefined与null的区别?var aconsole.log(a)  // undefineda = nullconsole.log(a) // null

什么时候给变量赋值为null?

  • 初始赋值,表明将要赋值为对象。因为 typeof null === "Object"
  • 结束前,让对象成为垃圾对象(被垃圾回收器回收)
//起始var b = null  // 初始赋值为null, 表明将要赋值为对象//确定对象就赋值b = ['atguigu', 12]//最后b = null // 让b指向的对象成为垃圾对象(被垃圾回收器回收)

严格区别变量类型数据类型

  • 数据的类型:

    • 基本类型
    • 对象类型
  • 变量的类型(变量内存值的类型)

    • 基本(值)类型:保存的就是基本类型的数据
    • 引用类型:保存的是地址值

理解实例类型

// 实例: 实例对象// 类型: 类型对象function Person (name, age) {// 构造函数  类型  this.name = name  this.age = age}var p = new Person('tom', 12) // 根据类型创建的实例对象