fe-interview
fe-interview copied to clipboard
[js] 第333天 用js实现typeof的功能
function getType(obj){
return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase()
}
function getType(v) {
return v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name
}
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'
}
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'
}
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] }