JavaScript数据类型
JavaScript根本数据类型/原始数据类型/值类型(六种)
Null
Undefined
String
Number
Boolean
Symbol
注:
Symbol
是 ES6 引入了一种新的原始数据类型,示意举世无双的值。注:在es10中退出了原始数据类型
BigInt
,现已被最新Chrome反对
console.log(BigInt) // ƒ BigInt() { [native code] }console.log(typeof 1n) // bigintconsole.log(1n instanceof BigInt) //falseconsole.log(Object.prototype.toString.call(1n)) //[object BigInt]console.log(jQuery.type(1n)) // bigint
BigInt MDN
援用数据类型/对象数据类型(一种)
Object
Array
Function
Date
RegExp
注:除了Array
、Function
、Date
、RegExp
属于非凡的对象数据类型。
包装类型(三种)
Number
String
Boolean
const Booln = new Boolean(true)console.log(Booln) // Boolean {true}console.log(Booln === true) // falseconsole.log(typeof Booln) // objectconsole.log(Booln instanceof Object) // trueconsole.log(Object.prototype.toString.call(Booln)) // [object Boolean]console.log(Object.prototype.toString(Booln)) // [object Object]const str = new String('123')console.log(str) // String {"123"}console.log(str === '123') // falseconsole.log(typeof str) // objectconsole.log(str instanceof Object) // trueconsole.log(Object.prototype.toString.call(str)) // [object String]console.log(Object.prototype.toString(str)) // [object Object]const num = new Number(123)console.log(num) // String {"123"}console.log(num === 123) // falseconsole.log(typeof num) // objectconsole.log(num instanceof Object) // trueconsole.log(Object.prototype.toString.call(num)) // [object Number]console.log(Object.prototype.toString(num)) // [object Object]
1、援用类型和根本包装类型的区别就是对象的生存期;
2、主动创立的根本包装类型的对象,只存在于一行代码的执行霎时,而后立刻被销毁;
3、意味着咱们不能在运行时为根本类型增加属性和办法。
根本类型的字面量写法,很显著不能为根本类型增加属性和办法
const s1 = 'name.Lee's1.name = 'lee's1.age = function () { return 100}console.log(s1.substring(5)) // =>Leeconsole.log(typeof s1) // stringconsole.log(s1.name) // undefinedconsole.lgo(s1.age()) // Uncaught TypeError: s1.age is not a function
new运算符写法,既能增加办法属性,还能应用它的内置办法substring
const s2 = new String('name.Lee')s2.name = 'lee's2.age = function () { return 100}console.log(s2.substring(5)) // =>Leeconsole.log(typeof s2) // objectconsole.log(s2.name) // leeconsole.log(s2.age()) // 100
null
和undefined
的区别
undefined与null的区别---阮一峰
笔记:
1、在!
运算符解决后,主动转成true
console.log(!null) // trueconsole.log(!undefined) // trueconsole.log(!undefined === !null) // true
2、相等,然而不全等
console.log(null == undefined) // trueconsole.log(null === undefined) // false
3、最后设计:null
是一个示意"无"的对象,转数值为0
;undefined
是一个示意"无"的原始值,转数值为NaN
。
console.log(Number(undefined)) //NaNconsole.log(Number(5 + undefined)) //NaNconsole.log(Number(null)) //0console.log(Number(5 + null)) //5
4、两者typeof
区别:
console.log(typeof undefined) //undefinedconsole.log(typeof null) //object
5、当初的用法:null
:示意"没有对象",即此处不应该有值。(示意被赋值过的对象,刻意把一个对象赋值为null
,成心示意其为空,不应有值。)
(1)作为函数的参数,示意该函数的参数不是对象;
(2)作为对象原型链的起点。undefined
:示意"短少值",即此处应该有一个值,然而没有定义。
(1)变量被申明了,但没有赋值时,就等于undefined;
(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined;
(3)对象没有赋值的属性,该属性的值为undefined;
(4)函数没有返回值时,默认返回undefined。
判断JavaScript数据类型的形式(四种)
typeof
instanceof
toString
jquery
typeof
实用场景:
console.log(typeof undefined) //undefinedconsole.log(typeof 123) //numberconsole.log(typeof '123') //stringconsole.log(typeof true) //booleanconsole.log(typeof Symbol()) //symbolconsole.log(typeof function () {}) //function
不实用场景:
console.log(typeof new Date()) //objectconsole.log(typeof /^\d*$/) //objectconsole.log(typeof {}) //objectconsole.log(typeof []) //objectconsole.log(typeof null) //object
面试题:
let foo = function bar() { return 123}console.log(typeof bar) // undefinedconsole.log(typeof foo)// console.log(typeof bar()) // Uncaught ReferenceError: bar is not definedconsole.log(typeof foo()) // number
instanceof
注:不适宜做类型的判断。
console.log([] instanceof Array) //trueconsole.log([] instanceof Object) //trueconsole.log(new Date() instanceof Date) //trueconsole.log(new Date() instanceof Object) //trueconsole.log(new RegExp() instanceof RegExp) //trueconsole.log(new RegExp() instanceof Object) //trueconsole.log(123 instanceof Number) //falseconsole.log(123 instanceof Object) //falseconsole.log('123' instanceof String) //falseconsole.log(false instanceof Boolean) //false
toString
、Object.prototype.toString.call()
toString
:每一个援用类型都有toString
办法,默认状况下。toString()
办法被每个object
对象继承。如果此办法在自定义中未被笼罩。toString()
返回'[object type]',type是对象的类型。注:
Object.prototype.toString.call()
是最罕用判断类型的办法。
各个类型判断
console.log(Object.prototype.toString.call('123')) //[object String]console.log(Object.prototype.toString.call(123)) //[object Number]console.log(Object.prototype.toString.call(null)) //[object Null]console.log(Object.prototype.toString.call(undefined)) //[object Undefined]console.log(Object.prototype.toString.call(true)) //[object Boolean]console.log(Object.prototype.toString.call([])) //[object Array]console.log(Object.prototype.toString.call({})) //[object Object]console.log(Object.prototype.toString.call(function () {})) //[object Function]console.log(Object.prototype.toString.call(Symbol())) //[object Symbol]console.log(Object.prototype.toString.call(-1n)) //[object BigInt]
非凡的
console.log(Object.prototype.toString.call(new String())) //[object String]console.log(Object.prototype.toString.call(new Object())) //[object Object]console.log(Object.prototype.toString.call(new Date())) //[object Date]console.log(Object.prototype.toString.call(new RegExp())) //[object RegExp]console.log(Object.prototype.toString.call(new Array())) //[object Array]console.log(Object.prototype.toString.call(new Error())) //[object Error]console.log(Object.prototype.toString.call(Math)) //[object Math]console.log(Object.prototype.toString.call(JSON)) //[object JSON]console.log(Object.prototype.toString.call(window)) //[object Window]
JavaScript数据类型