JavaScript手写系列(六)(手写判断对象的数据类型)

15 阅读1分钟

Object.prototype.toString.call()来判断对象的数据类型

function isType(type, target) {
    return `[object ${type}]` === Object.prototype.toString.call(target)
}

// 闭包版本
function isType(type) {
    return function(target) {
        return `[object ${type}]` === Object.prototype.toString.call(target)
    }
}