JS小tips 之 toString返回变量类型

  • Object.prototype的toString()能够返回变量类型
  • 而通常都重写了这个方法
  • 借助call()
            function classOf(x){
                if(x === null) return "NULL";
                if(x === undefined) return undefined;
                return Object.prototype.toString.call(x).slice(8,-1);
            }
    
            var x = 13;
            console.log(classOf(x));
相关文章
相关标签/搜索