欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-05 15:18:45
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 instanceo... ......
 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