JavaScript基础_类型判断

类型

1. 基础类型种类

JavaScript中的5种基本类型:

  • Undefined
  • Null
  • Boolean
  • Number
  • String

2. 引用类型

定义: 引用类型是指可能由多个值构成的对象
JavaScript中对象都是引用类型,而Function是一种特殊的对象(也是引用类型)
引用类型形成方式:

  • new关键字,包括对基本类型使用new关键字
  • {}对象定义,如var obj={}
  • 函数定义,如function func(){}
  • 函数赋值,如var funb = func;

3. typeof返回值

类型 返回值
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Symbol "symbol"
函数对象 "function"
任何其他对象 "object"

4. 类型判断

  • typeof
  • a instanceof 类型
  • a.constructor === 类型
  • Object.prototype.toString.call(a)
定义 typeof prototype
var a = undefined; typeof(a); //"undefined" Object.prototype.toString.call(a); //"[object Undefined]"
var b = null; typeof(b); //"object" Object.prototype.toString.call(b); //"[object Null]"
var c = true; typeof(c); //"boolean" Object.prototype.toString.call(c); //"[object Boolean]"
var d = 1; typeof(d); //"number" Object.prototype.toString.call(d); //"[object Number]"
var e = "1"; typeof(e); //"string" Object.prototype.toString.call(e); //"[object String]"
var f = function funF(){}; typeof(f); //"function" Object.prototype.toString.call(f); //"[object Function]"
var g = new Boolean(); typeof(g); //"object" Object.prototype.toString.call(g); //"[object Boolean]"
var h = new Number(); typeof(h); //"object" Object.prototype.toString.call(h); //"[object Number]"
var i = new String(); typeof(i); //"object" Object.prototype.toString.call(i); //"[object String]"
var j = {}; typeof(j); //"object" Object.prototype.toString.call(j); //"[object Object]"
var k = []; typeof(k); //"object" Object.prototype.toString.call(k); //"[object Array]"
var l = new Date(); typeof(l); //"object" Object.prototype.toString.call(l); //"[object Date]"

猜你喜欢

转载自www.cnblogs.com/full-stack-engineer/p/9569981.html
今日推荐