Number
数值分为整数和浮点数,可使用十进制,八进制,十六进制和科学计数法来表示。
var a = 1;
var b = 1.1;
var c = 067;
var d = 0xa;
var e = 1.23e7;console.log(a, b, c, d, e);
NaN
表示一个非数值,任何涉及到 NaN 的操作都会返回 NaN,NaN 与任何值都不相等(包括自身)。
BigInt
BigInt用于表示任意长度的整数。
number类型无法安全地表示大于 $$2{53}-1$$(即`9007199254740991`),或小于$$-2-1$$的整数。
bigInt 类型与 number 类型不兼容,不能直接计算。
const bigInt = BigInt(1234567890123456789012345678901234567890);
String
String 类型必须被括在引号里。
let str = "Hello"; // 双引号
let str2 = "Single quotes are ok too"; // 单引号
let phrase = `can embed another ${str}`; // 反引号
Boolean
只有两个值:true和false
Null
表示无,空,未知。
Undefined
表示未被赋值。
var str;
console.log(str); // undefined
Symbol
表示唯一的标识符。
let user = {name: "zhang",
};
var id = Symbol("id");user[id] = 1;console.log(user, user[id], user.id);
隐藏属性
全局 Symbol.for
通过相同的description获取相同的symbol。
let id = Symbol.for("id"); // 如果该 symbol 不存在,则创建它
let idAgain = Symbol.for("id");
console.log(id === idAgain);
Symbol.keyFor
和Symbol.for相反,通过symbol获取description。
Symbol.keyFor 只针对 Symbol.for 创建的 symbol 有效。
let id = Symbol.for("id");
let name = Symbol("id");
console.log(Symbol.keyFor(id), Symbol.keyFor(name));
- Symbol.hasInstance :当其他对象使用 instanceof 判断是否为该对象的实例时会调用。
- Symbol.isConcatSpeardable:使用 cancat()是否展开。
- Symbol.species:可以手动设置衍生对象的构造函数。
- Symbol.match:调用 match() 时。
- Symbol.search:调用 search()时。
- Symbol.replace:调用 replace()时.
- Symbol.split:调用 split()时。
- Symbol.iterator:调用遍历器方法。
- Symbol.toPrimitive:对象转换为原始类型时调用。
- Symbol.toStringTag :调用 toString()时可自定义
- Symbol.unscopables :被 with 排除的属性。
Object 类型
储存数据集合和更复杂的实体。
6 种类型判断方法
typeof
只能识别原始类型和引用类型。typeof x和 typeof(x)相同,这里的括号不是typeof的一部分。它是数学运算分组的括号。
JavaScript 编程语言的设计错误,JavaScript 在存储数据的时候会转换成 32 位存储,null 的标签类型和 object 一样都是 000 链接。
console.log(typeof 1);
console.log(typeof "1");
console.log(typeof undefined);
console.log(typeof true);
console.log(typeof Symbol());
console.log(typeof null);
console.log(typeof []);
console.log(typeof {});
console.log(typeof console);
console.log(typeof console.log);
constructor
指向当前实例的构造函数。
let str = "Covid-19";
console.log(str.constructor);let number = 123;
console.log(number.constructor);let arr = [1, 2, 3];
console.log(arr.constructor);let fun = function () {};
console.log(fun.constructor);let obj = {};
console.log(obj.constructor);
instanceof
在原型链上查找其是否为构造函数实例。
原始类型类型在 JavaScript 中是没有原型链的。所以 instanceof 操作符对原始类型来说只会返回 false。
let arr = [1, 2, 3];
console.log(arr instanceof Array);let fun = function () {};
console.log(fun instanceof Function);let obj = {};
console.log(obj instanceof Object);let number = 123;
console.log(number instanceof Number);let string = "123";
console.log(number instanceof String);let boolean = true;
console.log(number instanceof Boolean);
Object.prototype.toString
可以很好的判断数据类型,封装成方法即可。
console.log(Object.prototype.toString({}));
console.log(Object.prototype.toString.call({}));
console.log(Object.prototype.toString.call(1));
console.log(Object.prototype.toString.call("1"));
console.log(Object.prototype.toString.call(true));
console.log(Object.prototype.toString.call(function () {}));
console.log(Object.prototype.toString.call(null));
console.log(Object.prototype.toString.call(undefined));
console.log(Object.prototype.toString.call(/123/g));
console.log(Object.prototype.toString.call(new Date()));
console.log(Object.prototype.toString.call([]));
鸭子类型检测
通过检查自身特定属性来判断
let str = "Covid-19";
console.log(str.toLowerCase());let arr = [1, 2, 3];
console.log(arr.join(","));
等比较
console.log(null === null);console.log(undefined === void 0);