JavaScript数据类型JavaScript基本数据类型/原始数据类型/值类型(六种)NullUndefinedStringNumberBooleanSymbol注:Symbol是ES6引入的一种新的原始数据类型,代表一个唯一的值。注意:es10中加入了原始数据类型BigInt,现在最新的Chrome已经支持console.log(BigInt)//?BigInt(){[nativecode]}console.log(typeof1n)//bigintconsole。log(1ninstanceofBigInt)//falseconsole.log(Object.prototype.toString.call(1n))//[objectBigInt]console.log(jQuery.type(1n))//bigintBigIntMDN引用数据类型/对象数据type(oneSpecies)ObjectArrayFunctionDateRegExp注:除了Array,Function、Date、RegExp都是特殊的对象数据类型。封装类型(三种)NumberStringBooleanconstBooln=newBoolean(true)console.log(Booln)//Boolean{true}console.log(Booln===true)//falseconsole.log(typeofBooln)//objectconsole.log(BoolninstanceofObject)//trueconsole.log(Object.prototype.toString.call(Booln))//[objectBoolean]console.log(Object.prototype.toString(Booln))//[objectObject]conststr=newString('123')console.log(str)//String{"123"}console.log(str==='123')//falseconsole.log(typeofstr)//objectconsole.log(strinstanceofObject)//trueconsole.log(Object.prototype.toString.call(str))//[objectString]console.log(Object.prototype.toString(str))//[objectObject]constnum=newNumber(123)console.log(num)//String{"123"}console.log(num===123)//falseconsole.log(typeofnum)//objectconsole.log(numinstanceofObject)//trueconsole.log(Object.prototype.toString.call(num))//[objectNumber]console.log(Object.prototype.toString(num))//[objectObject]1、引用类和基础包类的分区就它是对象的生命周期;2、自动创建的基本封装类型的对象只在一行代码执行的那一刻存在,然后立即销毁;3.表示我们不能在运行时给基本类型添加属性和方法字面写,显然不能给基本类型添加属性和方法consts1='name.Lee's1.name='lee's1.age=function(){return100}console.log(s1.substring(5))//=>Leeconsole.log(typeofs1)//stringconsole.log(s1.name)//undefinedconsole.lgo(s1.age())//UncaughtTypeError:s1.ageisnotafunctionnewoperator,Youcanaddmethodattributesanduseitsbuilt-inmethodsubstringconsts2=newString('name.Lee')s2.name='lee's2.age=函数(){return100}console.log(s2.substring(5))//=>Leeconsole.log(typeofs2)//objectconsole.log(s2.name)//leeconsole.log(s2.age())//100null和undefined的区别undefined和null的区别---阮一峰笔记:1.!之后运算符被处理后,它会自动转换为trueconsole.log(!null)//trueconsole.log(!undefined)//trueconsole.log(!undefined===!null)//true2,等于,但不等于console.log(null==undefined)//trueconsole.log(null===undefined)//false3,设计初衷:null是一个代表“无”的对象,值为0;undefined是表示“无”的原始值,将值转换为NaN。console.log(Number(undefined))//NaNconsole.log(Number(5+undefined))//NaNconsole.log(Number(null))//0console.log(Number(5+null))//54、两者typeof的区别:console.log(typeofundefined)//undefinedconsole.log(typeofnull)//object5,当前用法:null:表示“没有对象”,即这里应该没有值。(表示已经赋值的对象,故意给一个对象赋值null,故意表示它是空的,不应该有值。)(1)作为函数的参数,表示函数的参数不是一个对象;(2)作为对象链的原型端。undefined:表示“缺失值”,即这里应该有一个值,但是没有定义。(1)当变量被声明但没有赋值时,等于undefined;(2)函数调用时,该提供的参数没有提供,参数等于undefined;(3)对象没有赋值属性,属性值未定义;(4)当函数没有返回值时,默认返回undefined。JavaScript数据类型判断方法(四种)typeofinstanceoftoStringjquerytypeof适用场景:console.log(typeofundefined)//undefinedconsole.log(typeof123)//numberconsole.log(typeof'123')//stringconsole.log(typeoftrue)//booleanconsole.log(typeofSymbol())//symbolconsole.log(typeoffunction(){})//函数不适用场景:console.log(typeofnewDate())//objectconsole.log(typeof/^\d*$/)//objectconsole.log(typeof{})//objectconsole.log(typeof[])//objectconsole.log(typeofnull)//对象面试题:letfoo=functionbar(){return123}console.log(typeofbar)//undefinedconsole.log(typeoffoo)//console.log(typeofbar())//UncaughtReferenceError:barisnotdefinedconsole.log(typeoffoo())//numberinstanceofnote:不适合做类型判断。console.log([]instanceofArray)//trueconsole.log([]instanceofObject)//trueconsole.log(newDate()instanceofDate)//trueconsole.log(newDate()instanceofObject)//trueconsole。log(newRegExp()instanceofRegExp)//trueconsole.log(newRegExp()instanceofObject)//trueconsole.log(123instanceofNumber)//falseconsole.log(123instanceofObject)//falseconsole.log('123'instanceofString)//falseconsole.log(falseinstanceofBoolean)//falsetoString,Object.prototype.toString.call()toString:默认情况下,每个引用类型都有一个toString方法。toString()方法被每个对象对象继承。如果这个方法在自定义中没有被覆盖。toString()返回“[objecttype]”,其中type是对象的类型。注意:Object.prototype.toString.call()是最常用的判断类型的方法。各个类型判定console.log(Object.prototype.toString.call('123'))//[objectString]console.log(Object.prototype.toString.call(123))//[objectNumber]console.log(Object.prototype.toString.call(null))//[objectNull]console.log(Object.prototype.toString.call(undefined))//[objectUndefined]console.log(Object.prototype.toString.call(true))//[对象布尔值]console.log(Object.prototype.toString.call([]))//[对象数组]console.log(Object.prototype.toString.call({}))//[objectObject]console.log(Object.prototype.toString.call(function(){}))//[objectFunction]console.log(Object.prototype.toString.call(Symbol()))//[objectSymbol]console.log(Object.prototype.toString.call(-1n))//[objectBigInt]特殊的console.log(Object.prototype.toString.call(newString()))//[objectString]console.log(Object.prototype.toString.call(newObject()))//[objectObject]console.log(Object.prototype.toString.call(newDate()))//[objectDate]console.日志(对象.prototype.toString.call(newRegExp()))//[objectRegExp]console.log(Object.prototype.toString.call(newArray()))//[objectArray]console.log(Object.prototype.toString.call(newError)()))//[对象错误]console.log(Object.prototype.toString.call(Math))//[对象数学]console.log(Object.prototype.toString.call(JSON))//[对象JSON]console.log(Object.prototype.toString.call(window))//[objectWindow]JavaScript数据类型
