fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[js] 第333天 用js实现typeof的功能

Open haizhilin2013 opened this issue 4 years ago • 5 comments

第333天 用js实现typeof的功能

作者:Indomi

我也要出题

haizhilin2013 avatar Mar 13 '20 21:03 haizhilin2013

        function getType(obj){
            return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase()
        }

longhui520 avatar Mar 14 '20 12:03 longhui520

function getType(v) {
  return v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name
}

vizoy avatar Nov 18 '20 10:11 vizoy

const typeofFun = (value) => {
  const type = Object.prototype.toString.call(value).replace(/\[object |]/g, '').toLowerCase()
  const dic = ['undefined', 'number', 'boolean', 'symbol', 'string', 'function', 'bigint']
  if (dic.includes(type)) return type
  return 'object'
}

wind8866 avatar Mar 30 '22 09:03 wind8866

function typeOf(value) {
    let type = Object.prototype.toString.call(value).slice(8,-1).toLowerCase()
    if(type.match(/^(function | undefined | number | symbol | string | bigint)$/)) return type
    return 'object'
}

mohaixiao avatar Jun 21 '22 08:06 mohaixiao

function myTypeOf(params) { const result = Object.prototype.toString.call(params).slice(8, -1).toLowerCase() let map = { number: 'number', array: 'Array', function: 'Function', undefined: 'undefined', string: 'String', object: 'Object', symbol: 'Symbol', null: 'Null', boolean: 'Boolean' } return map[result] }

xiaoqiangz avatar Sep 16 '22 09:09 xiaoqiangz