前端面试题 - JS篇

 1. js判断数据类型有哪些,怎样判断数据类型

(1)数据类型有:undefined、null、Boolean、Number、String、Object、BigInt、Symbol

(2)判断类型的方法(这里加了上数组和函数)

        ① typeof ,缺点:null 和 数组 在typeof 中判断为'object'

typeof undefined === 'undefined'
typeof null === 'object'
typeof true === 'boolean'
typeof 22 == 'number'
typeof 'abc' === 'string'
typeof Symbol() === 'symbol'

// 判断特殊
typeof {} === 'object'
typeof [] === 'object'

// function 也能通过typeof 判断
typeof function a() {} === 'function'

        ② Object.prototype.toString.call() ,这个方法能判断所有类型,所以比较常用

Object.prototype.toString.call(undefined) === '[object Undefined]'
Object.prototype.toString.call(null) === '[object Null]'
Object.prototype.toString.call(true) === '[object Boolean]'
Object.prototype.toString.call(123) === '[object Number]'
Object.prototype.toString.call('abc') === '[object String]'
Object.prototype.toString.call(Symbol()) === '[object Symbol]'

// 非基础数据类型
Object.prototype.toString.call([1,2,3]) === '[object Array]'
Object.prototype.toString.call(function a(){}) === '[object Function]'

2. 实现一个bind函数

Function.prototype.newBind = function(context) {
  var arg = Array.prototype.slice.call(arguments,1)
  const me = this
  return function (newArg) {
    arg = arg.concat(Array.prototype.slice.call(newArg));
    me.apply(context, arguments)
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值