Get the name of an object's type

前端 未结 20 2302
忘掉有多难
忘掉有多难 2020-11-21 22:37

Is there a JavaScript equivalent of Java\'s class.getName()?

20条回答
  •  猫巷女王i
    2020-11-21 22:58

    Fairly Simple!

    • My favorite method to get type of anything in JS
    function getType(entity){
        var x = Object.prototype.toString.call(entity)
        return x.split(" ")[1].split(']')[0].toLowerCase()
    }
    
    • my favorite method to check type of anything in JS
    function checkType(entity, type){
        return getType(entity) === type
    }
    

提交回复
热议问题