手写 typeof

个人练习,代码如下:

function myTypeof(o,type){
    const types = {
        'string': true,
        'number': true,
        'symbol': true,
        'bigint': true,
        'function': true,
        'boolean': true,
        'undefined': true,
        'null': false,
        'array': false,
        'object': false,
    }
    let k = Object.prototype.toString.call(o).toLowerCase().slice(8,-1)
    return types[k]?k:'object'
}

 

posted @ 2023-05-29 22:38  睡成蛆  阅读(40)  评论(0编辑  收藏  举报