es6语法浅析

1、let和const

作用域:
let在es6中是块级作用域即在当前块中有效

let name = 'zach'
while (true) {
	let name = 'obama'
	console.log(name)  //obama
	break
}
console.log(name)  //zach

但在同一个块中不能重复定义(可在不同块中定义)

对比var,var属于函数的或者全局的,只能定义一次

const是用来定义常量的,使用时初始化且不可修改,也是块级作用域

2、解构赋值

解构是ES6提供的语法糖,其实内在是针对可迭代对象的Iterator接口,通过遍历器按顺序获取对应的值进行赋值

数组结构:
// 情况1,变量和值一一对应
let arr = [5, 9, 10];
let [a, b, c] = arr;
console.log(a, b, c); // 输出 5 9 10

// 情况2,变量多,值少
let arr = [5, 9, 10];
let [a, b, c, d] = arr;
console.log(a, b, c, d); // 输出 5 9 10 undefined

// 情况3,变量少,值多
let arr = [5, 9, 10, 8, 3, 2];
let [a, b] = arr;
console.log(a, b); // 5, 9

// 情况4,按需取值
let arr = [5, 9, 10, 8, 3, 2];
let [, , a, , b] = arr; // 不需要用变量接收的值,用空位占位
console.log(a, b); // 10, 3 

// 情况5,剩余值
let arr = [5, 9, 10, 8, 3, 2];
let [a, b, ...c] = arr; // ...c 接收剩余的其他值,得到的c是一个数组
console.log(a, b, c); 
// 结果:
// a = 5, 
// b = 9, 
// c = [10, 8, 3, 2]

// 情况6,复杂的情况,只要符合模式,即可解构
let arr = ['zhangsan', 18, ['175cm', '65kg']];
let [, , [a, b]] = arr;
console.log(a, b); // 175cm 65kg

对象解构:
// 情况1,默认要求变量名和属性名一样
let { foo, bar } = {foo: 'aaa', bar: 'bbb'};
console.log(foo, bar); // aaa, bbb
let {a, c} = {a: 'hello', b: 'world'};
console.log(a, c); // hello, undefined

// 情况2,可以通过:为变量改名
let {a, b:c} = {a: 'hello', b: 'world'};
console.log(a, c); // hello, world

// 情况3,变量名和属性名一致即可获取到值,不一定要一一对应
let {b} = {a: 'hello', b: 'world'};
console.log(b); // world
// 此时,没有定义变量a,所以使用a会报错

// 情况4,剩余值
let obj = {name:'zs', age:20, gender:'男'};
let {name, ...a} = obj;
console.log(name, a);
// 结果:
// name = zs
// a = {age: 20, gender: "男"};

// 情况5,复杂的情况,只要符合模式,即可解构
let obj = {
	name: 'zhangsan',
	age: 22,
	dog: {
		name: '毛毛',
		age: 3
	}
};
let {dog: {name, age}} = obj;
console.log(name, age); // 毛毛 3

3、函数与参数

箭头函数:
使用箭头定义函数、目的是简化函数定义
// 非箭头函数
let fn = function (x) {
	return x * 2;
}
// 箭头函数,等同于上面的函数
let fn = (x) => {
	return x * 2;
}
//继续简化——形参可以省略括号
let fn = x => {
	return x * 2;
}
//函数体一行时可以省略大括号,并且写在一行上
let fn = x => return x * 2;


默认参数:
function fn(x, y = 'world') {
	console.log(x, y);
}
调用函数,不传的值时y的值默认world,如果传了就会覆盖这个值


rest参数:
以 …修饰最后一个参数,把多余的参数都放到一个数组中
/ 参数很多,不确定多少个,可以使用剩余参数
function fn(...values) {
	console.log(values); // [6, 1, 100, 9, 10]
}
// 调用
fn(6, 1, 100, 9, 10); 

4、内置函数的扩展

扩展运算符:
...可以把数组中的每一项展开
// 合并两个数组
let arr1 = [1, 2];
let arr2 = [3, 4];
let arr3 = [...arr1, ...arr2];
console.log(arr3); // [1, 2, 3, 4]


find()方法和findIndex()方法
find():用于查找数组中的值
findIndex():用于查找数组的下标,用法与find一样


includes()方法:
判断数组是否包含某个值,返回 true / false
参数1,必须,表示查找的内容
参数2,可选,表示开始查找的位置,0表示开头的位置


includes(), startsWith(), endsWith():
includes(str, [position]) 返回布尔值,表示是否找到了参数字符串
startsWidth(str, [position]) 返回布尔值,表示参数字符串是否在原字符串的头部或指定位置
endsWith(str, [position]) 返回布尔值,表示参数字符串是否在原字符串的尾部或指定位置。
console.log('hello world'.includes('e', 2)); // false 从位置2开始查找e,没有找到
console.log('hello world'.includes('e')); // true
console.log('hello world'.startsWith('h')); // 未指定位置,看开头是否是h,返回true
console.log('hello world'.startsWith('l', 2)); // 指定位置的字符是l,返回true
console.log('hello world'.endsWith('d')); // 未指定位置,结尾是d,返回true
console.log('hello world'.endsWith('r', 9)); // 指定位置的字符是r,返回true


repeat()方法:
repeat方法返回一个新字符串,表示将原字符串重复n次。
let bb = '147'.repeat(2);
console.log(bb);

padStart(),padEnd():
'x'.padStart(5, 'ab') // 'ababx'
'x'.padStart(4, 'ab') // 'abax'
'x'.padEnd(5, 'ab') // 'xabab'
'x'.padEnd(4, 'ab') // 'xaba'

5、新增对象Set

和java中set类似,不重复的值
size:属性,获取 set 中成员的个数,相当于数组中的 length
add(value):添加某个值,返回 Set 结构本身。
delete(value):删除某个值,返回一个布尔值,表示删除是否成功。
has(value):返回一个布尔值,表示该值是否为Set的成员。
clear():清除所有成员,没有返回值。
let set = new Set([4, 8, 9, 5, 4, 8, 4, 2]);
set.add(2);

6、字符串扩展
字符串拼接:
let name = ‘zs’;
let age = 18;
let msg = 我是${name},今年${age};
也可以在 中 调 用 函 数 f u n c t i o n f ( ) c o n s o l e . l o g ( ′ 5 5 ′ ) ; r e t u r n ′ ∣ ∣ ∣ ∣ ∣ ′ ; ‘ a s f a s 中调用函数 function f(){ console.log('55'); return '|||||'; } `asfas functionf()console.log(55);return;asfas{f()}sfas`

字符串编码:
在JavaScript中以utf-16格式存储字符串,每个字符2字节,对于4字节的字符会认为是2个字符,下面是解决办法
"𠮷".codePointAt().toString(16)
String.fromCodePoint(0x20BB7)

字符串遍历:使用for 。。of   遍历
for (let i of text) {
  console.log(i);
}

根据下标获取字符(提案)
es5:"".charAt(0)    //对于4字节的字符会乱码
es6:"".at(0)			//兼容4字节的字符,不会乱码

多行字符串的写法:
es5:var s = "asfasfas"
			  + "asfasfasf";
es6:let s = `sdfsdgsdfsd
			  fjsdfdlskfldsf
			  asfafssaf`;

7、正则扩展
点字符
点字符是定义除换行外的所有的单字符的,但是4字节字符的特殊性导致了bug,u字符表示以4字节字符来进行匹配
let s = “𠮷”;
/^.$/u.test(s);

字符编码转字符:
使用{}表示当前字符以四字节字符匹配
/\u{61}/.test('a') // false
/\u{61}/u.test('a') // true
/\u{20BB7}/u.test('𠮷') // true

量词:
/𠮷{3}/u.test("𠮷𠮷𠮷")

预定义模式:
\S是预定义模式,匹配所有不是空格的字符
/^\S$/u.test("𠮷");

i修饰符
在[a-z]范围内的字符
/[a-z]/i.test('\u212A')
/[a-z]/iu.test('\u212A')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值