1. 程式人生 > >JS小tips 之 toString返回變量類型

JS小tips 之 toString返回變量類型

變量類型 返回 proto fin col var func lasso call()

  • 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));

JS小tips 之 toString返回變量類型