共计 5223 个字符,预计需要花费 14 分钟才能阅读完成。
JavaScript 数据类型
JavaScript 根本数据类型 / 原始数据类型 / 值类型(六种)
Null
Undefined
String
Number
Boolean
Symbol
注:
Symbol
是 ES6 引入了一种新的原始数据类型,示意举世无双的值。注:在 es10 中退出了原始数据类型
BigInt
,现已被最新 Chrome 反对
console.log(BigInt) // ƒ BigInt() { [native code] } | |
console.log(typeof 1n) // bigint | |
console.log(1n instanceof BigInt) //false | |
console.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) // false | |
console.log(typeof Booln) // object | |
console.log(Booln instanceof Object) // true | |
console.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') // false | |
console.log(typeof str) // object | |
console.log(str instanceof Object) // true | |
console.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) // false | |
console.log(typeof num) // object | |
console.log(num instanceof Object) // true | |
console.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)) // =>Lee | |
console.log(typeof s1) // string | |
console.log(s1.name) // undefined | |
console.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)) // =>Lee | |
console.log(typeof s2) // object | |
console.log(s2.name) // lee | |
console.log(s2.age()) // 100 |
null
和 undefined
的区别
undefined 与 null 的区别 — 阮一峰
笔记:
1、在
!
运算符解决后,主动转成true
console.log(!null) // true | |
console.log(!undefined) // true | |
console.log(!undefined === !null) // true |
2、相等,然而不全等
console.log(null == undefined) // true | |
console.log(null === undefined) // false |
3、最后设计:
null
是一个示意 ” 无 ” 的对象,转数值为0
;undefined
是一个示意 ” 无 ” 的原始值,转数值为NaN
。
console.log(Number(undefined)) //NaN | |
console.log(Number(5 + undefined)) //NaN | |
console.log(Number(null)) //0 | |
console.log(Number(5 + null)) //5 |
4、两者
typeof
区别:
console.log(typeof undefined) //undefined | |
console.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) //undefined | |
console.log(typeof 123) //number | |
console.log(typeof '123') //string | |
console.log(typeof true) //boolean | |
console.log(typeof Symbol()) //symbol | |
console.log(typeof function () {}) //function |
不实用场景:
console.log(typeof new Date()) //object | |
console.log(typeof /^\d*$/) //object | |
console.log(typeof {}) //object | |
console.log(typeof []) //object | |
console.log(typeof null) //object |
面试题:
let foo = function bar() {return 123} | |
console.log(typeof bar) // undefined | |
console.log(typeof foo) | |
// console.log(typeof bar()) // Uncaught ReferenceError: bar is not defined | |
console.log(typeof foo()) // number |
instanceof
注:不适宜做类型的判断。
console.log([] instanceof Array) //true | |
console.log([] instanceof Object) //true | |
console.log(new Date() instanceof Date) //true | |
console.log(new Date() instanceof Object) //true | |
console.log(new RegExp() instanceof RegExp) //true | |
console.log(new RegExp() instanceof Object) //true | |
console.log(123 instanceof Number) //false | |
console.log(123 instanceof Object) //false | |
console.log('123' instanceof String) //false | |
console.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 数据类型