• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
GoForIt0818
博客园    首页    新随笔    联系   管理    订阅  订阅

判断字符串、数组、对象的数据类型

 1 var str1=new String('str1');
 2 var str2='str2';
 3 console.log(typeof str1);//object
 4 console.log(typeof str2);//string
 5 console.log(str1 instanceof String);//true
 6 console.log(str2 instanceof String);//false
 7 console.log(str1.constructor==String);//true
 8 console.log(str2.constructor==String);//true
 9 
10 var a = ['hello','world'];
11 console.log(typeof a);//object
12 console.log(a.toString());//hello,world
13 console.log(Object.prototype.toString.call(a));//[object Array]
14 console.log(Array.prototype.isPrototypeOf(a));//true
15 console.log(Array.isArray(a));//true
16 console.log(a instanceof Array);//true
17 console.log(a instanceof Object);//true
18 console.log(a.constructor==Array);//true
19 console.log(a.constructor==Object);//false
20 
21 var b = {'hello':'world'};
22 console.log(typeof b);//object
23 console.log(b.toString());//[object Object]
24 console.log(Object.prototype.toString.call(b));//[object Object]
25 console.log(Object.prototype.isPrototypeOf(b));//true
26 console.log(b instanceof Object);//true
27 console.log(b.constructor==Object);//true

 

posted @ 2018-08-22 16:29  GoForIt0818  阅读(489)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3