码迷,mamicode.com
首页 > Web开发 > 详细

JS小tips 之 toString返回变量类型

时间:2017-11-10 10:59:55      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:sof   fine   log   nbsp   define   color   turn   def   var   

  • 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返回变量类型

标签:sof   fine   log   nbsp   define   color   turn   def   var   

原文地址:http://www.cnblogs.com/ideal-idiot/p/7813126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!