首页 > 编程语言 > 详细

JavaScript探索--3

时间:2018-03-19 10:17:29      阅读:182      评论:0      收藏:0      [点我收藏+]
typeof与instanceof
  • 判断字符串类型可用typeOf,但判断数组,对象,正则,null时,返回的都是Object,所以可用instanceof来判断类型
    <script>
    var str = ‘Lee‘;
    alert(typeof str);//string
    var arr = [1,2,3];
    alert(typeof arr);//object
    alert(arr instanceof Array);//true
    </script>
  • 注意:当类型为string时,用 instanceof 判断的话会为 false
    <script>
    var str = ‘xu xiao dai‘;
    alert(str instanceof String);//false
    var strObj = new String(‘xu xiao dai‘);
    alert(strObj instanceof String);//true
    </script>

JavaScript探索--3

原文:http://blog.51cto.com/531651/2088308

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!