Skip to content

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

@haizhilin2013

Description

@haizhilin2013
Collaborator

第333天 用js实现typeof的功能

作者:Indomi

我也要出题

Activity

longhui520

longhui520 commented on Mar 14, 2020

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

vizoy commented on Nov 18, 2020

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

wind8866 commented on Mar 30, 2022

@wind8866
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'
}
mohaixiao

mohaixiao commented on Jun 21, 2022

@mohaixiao
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'
}
xiaoqiangz

xiaoqiangz commented on Sep 16, 2022

@xiaoqiangz

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]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@xiaoqiangz@vizoy@wind8866@longhui520

        Issue actions

          [js] 第333天 用js实现typeof的功能 · Issue #2059 · haizlin/fe-interview