1. 首页
  2. SEO技术

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

gooood个人博客网站

JavaScript

var str1=new String('str1'); var str2='str2'; console.log(typeof str1);//object console.log(typeof str2);//string console.log(str1 instanceof String);//true console.log(str2 instanceof String);//false console.log(str1.constructor==String);//true console.log(str2.constructor==String);//true var a = ['hello','world']; console.log(typeof a);//object console.log(a.toString());//hello,world console.log(Object.prototype.toString.call(a));//[object Array] console.log(Array.prototype.isPrototypeOf(a));//true console.log(Array.isArray(a));//true console.log(a instanceof Array);//true console.log(a instanceof Object);//true console.log(a.constructor==Array);//true console.log(a.constructor==Object);//false var b = {'hello':'world'}; console.log(typeof b);//object console.log(b.toString());//[object Object] console.log(Object.prototype.toString.call(b));//[object Object] console.log(Object.prototype.isPrototypeOf(b));//true console.log(b instanceof Object);//true console.log(b.constructor==Object);//true

本文来自SEO排名优化_SEO技术_SEO资讯网,经授权后发布,转载请联系原作者。原文链接:http://www.ylzug.com/js/572.html