JS数组解构
解构赋值
const arr = [1, 2, 3];
let a, b, c;
[a, b, c] = arr; // 数组的解构赋值
console.log(a, b, c); // 1 2 3
const arr = [1, 2, 3];
let [a, b, c, d=4] = arr; // d=4 默认值
console.log(a, b, c, d); // 1 2 3 4
const arr = [1, 2, 3, 4];
let [a, b, ...c] = arr;
console.log(a, b, c); // 1 2 [3, 4]
// 通过解构赋值交换变量值
let x = 1, y = 2;
[x, y] = [y, x]; // [x, y]表示解构赋值,[y, x]是一个新数组
console.log(x, y); // 2 1
JS对象解构
解构赋值
const obj = { name: 'x', age: 1 };
let name, age;
({name, age} = obj);
console.log(name, age); // x 1
const obj = { name: 'x', age: 1 };
let {name, age} = obj;
console.log(name, age); // x 1
// 使用别名变量解构赋值
const obj = { name: 'x', age: 1 };
let { name:x, age:y } = obj;
console.log(x, y); // x 1
// 使用别名变量/默认值变量解构赋值
const obj = { name: 'x', age: 1 };
let { name:x, age:y, gender='f' } = obj;
console.log(x, y, gender); // x 1 f
JS对象序列化
通过序列化后,可以实现在不同程序间的数据传递
// 序列化,对象转JSON字符串
const obj = { name: 'x' };
const objstr = JSON.stringify(obj);
// 反序列化,JSON字符串转对象
const jsonstr = '{"name":"x"}';
const obj = JSON.parse(jsonstr);
JS Map
// 创建Map
const map = new Map();
// 添加数据,key不限制数据类型
map.set('key', 'value');
map.set({}, 'obj');
map.set(NaN, 'NaN');
// 获取数据
map.get('key');
// 删除数据
map.delete('key');
// 检查元素
map.has('key');
// 清空数据
map.clear();
// 数据大小
map.size();
// Map转二维数组
const map = new Map();
map.set('name', 'x');
map.set('age', 10);
const arr = Array.from(map);
console.log(arr); // [['name', 'x'], ['age', 10]]
// 二维数据转Map
const arr = [['name', 'x'], ['age', 10]];
const map = new Map(arr);
// for...of...遍历Map
const arr = [['name', 'x'], ['age', 10]];
const map = new Map(arr);
for(const entry of map) {
console.log(entry); // ['name', 'x'] ['age', 10]
}
for(const [k, v] of map) {
console.log(k, v); // 'name' 'x' 'age' 10
}
// forEach 遍历Map
const arr = [['name', 'x'], ['age', 10]];
const map = new Map(arr);
map.forEach((k, v, m)=>{
console.log(k, v);
});
JS Set
// 创建Set
const set = new Set();
const set1 = new Set([10, 20]);
// 添加数据
set.add(10);
// 检查数据
set.has(10);
// 删除数据
set.delete(10);
// 数据大小
set.size();
// Set转组
const set = new Set();
const arr = [...set];
// 遍历Set
const set = new Set([10, 20]);
for(const s of set) {
console.log(s);
}
JS Date
const d = new Date();
console.log(d); // Thu Mar 21 2024 15:38:47 GMT+0800 (中国标准时间)
// MM/dd/yyyy hh:mm:ss
const d = new Date('3/21/2024 15:39:59');
console.log(d); // Thu Mar 21 2024 15:39:59 GMT+0800 (中国标准时间)
// yyyy-MM-ddThh:mm:ss
const d = new Date('2024-03-21T15:39:59');
console.log(d); // Thu Mar 21 2024 15:39:59 GMT+0800 (中国标准时间)
// 年份
d.getFullYear();
// 月份(0-11)
d.getMonth();
// 日期
d.getDate();
// 星期(0-6)
d.getDay();
// 时间戳(ms)
d.getTime();
// 时间戳(ms)
Date.now();
JS正则表达式
正则表达式,Regular Expression,又称规则表达式。
一、元字符
( ) [ ] \ { } ^ $ ? * + .
?,出现零或一次*,出现零或多次+,出现至少一次{n},对应零或多次{n,m},至少出现n次但不超过m次{n,},至少出现n次
二、预定义的特殊字符
\r,回车符
\n,换行符
\t,制表符
.,[^\n\r],除了换行符和回车符之外的任意字符\d,[0-9],数字字符\s,[\t\n\x0B\f\r],空白字符\w,[a-zA-Z0-9_],单词数字下划线
三、基础示例
//匹配(包含)即为true
var txt = 'abc123';
var reg = /b/;
console.log(reg.test(txt)); //true
//首字符匹配
var txt = 'abc123';
var reg = /^b/;
console.log(reg.test(txt)); //false,需要首字符为'b'才匹配
//末字符匹配
var txt = 'abc123';
var reg = /b$/;
console.log(reg.test(txt)); //false,需要末字符为'b'才匹配
//0或1次匹配
var txt = 'abc123';
var reg = /b?/;
console.log(reg.test(txt)); //true
//任意一次匹配
var txt = 'abc123';
var reg= /b*/;
console.log(reg.test(txt)); //true
//至少匹配一次
var txt = 'abc123';
var reg = /b+/;
console.log(reg.test(txt));
//指定匹配次数
var txt = 'abc123';
var reg = /b{2,3}/
console.log(reg.test(txt)); //false
//首尾同时匹配单个
var txt = 'abc213';
var reg = /^\w$/;
//首尾各自匹配
var txt = 'abc123';
var reg = /^\w+$/'
//匹配集合内任一个
var txt = 'abc123';
var reg = [a-zA-Z0-9_-+]
//匹配小数点
var txt = 'abc123';
var reg = /./;
//匹配任意字符,除了\r\n
var txt = 'abc123';
var reg = /./;
//匹配邮箱格式
//使用new RegExp的方式不支持预定义特殊字符,
//以字面量直接表示的则无限制
var txt = $('input[name="txt"]').val();
var reg = new RegExp();
reg = new RegExp('^[a-zA-Z0-9_-]+@[a-zA-Z]+(.[a-zA-Z]{2,}){1,}$');
console.log(reg.test(txt));
reg = /^[\w-]+@[a-zA-Z]+(.[a-zA-Z]{2,}){1,}$/;
console.log(reg.test(txt));
JS垃圾回收
当对象没有任何变量对其引用时,那么这个对象就会成为垃圾。
// {} -> Garbage
const obj = {}; // obj -> {}
obj = null; // obj -> null