一. 字符串扩展API
includes() 判断字符串是否包含指定的字符(有的话返回true 没有返回false)
startWidth() 判断是否以特定的字符开始
endWidth() 判断是否以特定的字符结束
例子 :
let nums='qwertyu';
console.log(nums.includes('t',3));
console.log(nums.startsWith('qwe'));
console.log(nums.endsWith('qwe'));
- 模板字符串
反引号``表示模板,模板中的内容格式通过${}
填充
二. 函数的扩展
- 参数的默认值
function foo(param='hello'){ //对形参赋值
console.log(param);
}
foo('hi'); //输出结果为 hi
foo(); //输出结果为 hello
- 参数的解构赋值
参数的解构赋值的三种形式
function foo({username,age}){
console.log(username,age);
}
foo({}); 输出undefined
function foo({username='lisi',age=23}={}){
console.log(username,age);
}
foo(); 输出 lisi 23
function foo({username='lisi',age=23}={}){
console.log(username,age);
}
foo({username:'www',age:11}); 输出 www 11
- rest参数
rest表示剩余参数,但不一定要使用rest作为参数
function foo(a,...param){
console.log(param);
}
foo(1,2,3); //输出结果 [2,3]
- …扩展运算符
对象中的扩展运算符(…)用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中
function foo(a,b,c,d,...param){
console.log(a+b+c+d);
}
foo(1,2,3,4,5); 输出结果为 10
使用扩展运算符合并数组
let arr1=[1,2,3];
let arr2=[4,5,6];
let arr3=[...arr1,...arr2];
console.log(arr3); 输出结果为[ 1, 2, 3, 4, 5, 6 ]