JavaScript数据类型

70 阅读3分钟

分类

  • 基本(值)类型
    • String: 任意字符串
    • Number: 任意的数字(含整数跟小数)
    • Boolean: true/false
    • undefined: undefined
    • null: null
    • Symbol:(es6新增/用于声明一个独一无二的值)
    • BigInt: (es6新增/用于超大整数的运算处理)
  • 对象(引用)类型
    • Object: 任意对象

判断

  • typeof:
    • 可以判断:基本类型(null除外)与function
    • 不能判断: null与object  object与array
  • instanceof:
    • 判断对象(引用)类型
  • constructor
    • 判断对象(引用)类型
  • toString
    • 判断所有类型(全能扛把子)

typeof是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型。返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 7 种:number、boolean、symbol、string、object、undefined、function 等。

typeof ''// string 有效
typeof 1// number 有效
typeof Symbol()// symbol 有效
typeof true//boolean 有效
typeof undefined; //undefined 有效
typeof null//object 无效
typeof [] ; //object 无效
typeof new Function()// function 有效
typeof new Date()//object 无效
typeof new RegExp()//object 无效

instanceof 是用来判断 A 是否为 B 的实例,返回布尔值(除了判断自身,instanceof 还会沿着原型链往上找,只要原型链上存在有 constructor 的实例(继承),返回结果true。)

var b1 = {
  b2: [1'abc'console.log],
  b3function () {
    console.log('b3')
    return function () {
      return 'xfzhang'
    }
  }
}
console.log(b1 instanceof Object, b1 instanceof Array// true  false
console.log(b1.b2 instanceof Array, b1.b2 instanceof Object// true true
console.log(b1.b3 instanceof Function, b1.b3 instanceof Object// true true

//判断b3是不是函数
console.log(typeof b1.b3==='function'// true
//判断b2数组的第三个元素是不是函数
console.log(typeof b1.b2[2]==='function')//true
//调用该函数
b1.b2[2](4)

console.log(b1.b3()())//输出xfzhang

constructor有时候我们只想判断当前对象是否为某构造函数的实例,而非连带原型链上的其他对象也一起判断。这种情况下,我们可以根据对象的 constructor 来进行判断:

function SuperType(){}
function SubType(){}
SubType.prototype = new SuperType();
// 把原型上的构造器指向自身,否则会判断错误
SubType.prototype.constructor = SubType;
let sub = new SubType();

sub.constructor === SubType    // true
sub.constructor === SuperType  // false

toString是 Object 的原型方法,调用该方法,默认返回当前对象的 [class] 。这是一个内部属性,其格式为 [object xx] ,其中 xx 就是对象的类型。
对于 Object 对象,直接调用 toString()  就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。

`Object.prototype.toString.call(``''``) ;   ``// [object String]`

`Object.prototype.toString.call(1) ;    ``// [object Number]`

`Object.prototype.toString.call(``true``) ; ``// [object Boolean]`

`Object.prototype.toString.call(Symbol()); ``//[object Symbol]`

`Object.prototype.toString.call(undefined) ; ``// [object Undefined]`

`Object.prototype.toString.call(``null``) ; ``// [object Null]`

`Object.prototype.toString.call(``new` `Function()) ; ``// [object Function]`

`Object.prototype.toString.call(``new` `Date()) ; ``// [object Date]`

`Object.prototype.toString.call([]) ; ``// [object Array]`

`Object.prototype.toString.call(``new` `RegExp()) ; ``// [object RegExp]`

`Object.prototype.toString.call(``new` `Error()) ; ``// [object Error]`

`Object.prototype.toString.call(document) ; ``// [object HTMLDocument]`

`Object.prototype.toString.call(window) ; ``//[object window]`

数据类型相关问题

undefined与null的区别?(面试常考)

  • undefined代表定义未赋值
  • null定义并赋值了, 只是值为null

什么时候给变量赋值为null呢?(常考)

  • 初始赋值, 表明将要赋值为对象
  • 结束前, 让对象成为垃圾对象(被垃圾回收器回收)

严格区别变量类型与数据类型?(了解)

  • 数据的类型
    • 基本类型
    • 对象类型
  • 变量的类型(变量内存值的类型)
    • 基本类型: 保存就是基本类型的数据
    • 引用类型: 保存的是地址值
// 1. undefined与null的区别?
var a
console.log(a)  // undefined
a = null
console.log(a) // null
//起始
var b = null  // 初始赋值为null, 表明将要赋值为对象
//确定对象就赋值
b = ['atguigu', 12]

//最后
b = null // 让b指向的对象成为垃圾对象(被垃圾回收器回收)