JavaScript确定值的类型

JavaScript确定值的类型

1.typeof运算符

typeof 123 // "number"
typeof '123' // "string"
typeof false // "boolean"

typeof undefined // "undefined"

typeof function(){} // "function"

typeof {} // "object"
typeof [] // "object"
typeof null // "object"

特殊说明

  • typeof可以用来检查一个没有声明的变量,而不报错

    v // ReferenceError: v is not defined
    typeof v // "undefined"
    
  • 在 JavaScript 内部,数组本质上只是一种特殊的对象。

  • null 也是一种特殊的对象。

2.instanceof运算符

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

var v = {}
v instanceof Object // true

var v = []
v instanceof Array // true
v instanceof Object // true

function f(){}
f instanceof Function // true
f instanceof Object // true

特殊说明

  • 构造函数即是函数,也是特殊的对象

    Function instanceof Function // true
    Function instanceof Object // true
    Object instanceof Function // true
    Object instanceof Object // true
    

自定义instanceof (面试常考)

function isInstanceof(obj, Type) {
  if (typeof Type !== 'function') {
    throw Error('Type 必须是一个构造函数!')
  }

  let pt = Type.prototype
  let proto = obj.__proto__;
  // let proto = Object.getPrototypeOf(obj);
  while(proto) {
    if (proto === pt) {
      return true
    }
    proto = proto.__proto__
    // proto = Object.getPrototypeOf(proto)
  }
  return false
}

function Foo(){}
function Person(){}
class Student extends Person{}

let p = new Person()
let s = new Student()

console.log(isInstanceof(p, Foo))       // false
console.log(isInstanceof(p, Person))    // true
console.log(isInstanceof(p, Object))    // true

console.log(isInstanceof(s, Student))  // true
console.log(isInstanceof(s, Person))  // true

console.log(isInstanceof(Foo, Function)) // true
console.log(isInstanceof(Foo, Object))   // true

Object.prototype.isPrototypeOf()

function isInstanceof(obj, Type) {
    return Object.prototype.isPrototypeOf.call(Type.prototype, obj)
}

3.Object.prototype.toString()

Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call('') // "[object String]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call([]) // "[object Array]"
Object.prototype.toString.call(function (){})  // [object Function]

自定义函数获取类型(面试常考)

function getDataType(obj) {
    return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
    // return Object.prototype.toString.call(obj).match(/\[object (.*?)]/)[1].toLowerCase();
    // return Object.prototype.toString.call(obj).replace(/^\[object (.+)]$/, '$1').toLowerCase();
}

console.log(getDataType(34.4))  // number
console.log(getDataType({}))    // object
console.log(getDataType([]))    // array
console.log(getDataType(function (){})) // function

Array

[] instanceof Array	// true
Array.isArray([])	// true

Object

function isObject(value) {
  return value === Object(value);
}

isObject([]) // true
isObject({}) // true
isObject(function(){} ) // true
isObject(true) // false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值