*Js有哪些数据类型?Js虽然没有类的概念,但是还是有两种;原始类型和引用类型原始类型原始类型用于保存一些简单的单一数据。js有5种基本类型:booleannumberstringnullundefined前三种行为类似,后两种有一点不同。识别原始类型的方法:typeofstrings/numbers/Booleans虽然都是原始类型,但是它们也有方法console.log(typeofnull)//objectnull是一个空对象指针console.log(value===null)//判断一个值是否为空console.log(undefined==null)//trueconsole.log(undefined===null)//falsevarstr='aabbcc'str.toLowerCase()//转换为更大的str.charAt(0)//获取第一个字符str.substrin(2,5)//2-4不包括结尾//numbervara=10a.toFixed(2)//10.00a.toString(16)//a//boobeanvarb=trueb.toString();//'true'引用类型引用类型指的是js中的对象,是你能找到的最接近语言类的东西。引用值是引用类型的实例,也是对象的同义词。函数实际上是一个引用值,包含数组的属性与包含函数的属性没有区别,只是函数可以运行。js中创建对象的方式有几种?第一种是使用new运算符和构造函数,它是用new创建的对象的函数——任何函数都可以是构造函数。构造函数首字母大写,以区别非构造函数。第二个对象字面量和数组字面量varobj={x:1}vararr=[12,3]第三个函数字面量functionfn(val){returnval}varreflect=newFunction('val','returnvalue')第四种正则表达式字面量形式varnum=/\d+/gvarnums=newRegExp("\\d+",g)识别引用类型该函数最容易识别引用类型,因为函数使用的返回值typeof运算符是“函数”;而识别其他引用类型总是返回对象,可以使用instanceof运算符来解决arr)//'object'console.log(fninstanceofFunction)//trueconsole.log(fninstanceofObject)//trueconsole.log(arrinstanceofArray)//trueconsole.log(objinstanceofObject)//trueconsole.log(Array.isArray(arr))//true是识别数组的最佳方式。原始封装类型会在读取一个string/array/boolean时自动创建,即一个stringnativemethodsstr=newString('aaf'),str.last,str.charAt(0)newNumber(10)newBoolean(false)typeof都是'object'varsobj=newString('haha')typeofsobj//objectsobjinstanceofObject//truesobjinstanceofString//truevarstr='haha'//不属于原来的封装类型临时对象只在读取时创建typeofstr//stringstrinstanceofString//falsestrinstanceofObject//false
