ES6常用语法(解构、字符串模板、字符串操作)

1.解构赋值(左右两边格式一致):

数组对应:

let [a,b] = ['11','22'];         console.log(a,b);

let [a,[b,c]] = ['11',['22','333']];         console.log(a,b);

json对应:

let {a,b,c} = {a:"111",b:"222",c:"333"};     console.log(a,b,c);

起别名:

let json = {name:"ddd",age:"12",job:"ads"}

let {a,b,c:dd} = json;     console.log(a,b,dd);

默认值:

let [a,b,c='默认值']= ['aa','bb'];    console.log(a,b,c);

交换位置:

let a = 1;let b = 3;

[a,b] = [b,a];   console.log(a,b)

函数传参:

例子1:

function show({a,b="你好"}){

    console.log(a,b);

}

show({a:1})

例子2:

function show({a="dd",b="你好"} = {}){

    console.log(a,b);

}

show()

2.字符串模板

let name = "你好";

let age = 18;

let str = `这个人叫 ${name} ,年龄是 ${age} `;

console.log(str);

3.字符串的操作:

字符串查找:

传统js:

let str = "asdfasdfasdfasdf"

console.log(str.indexOf('apple'));   //返回索引位置,没找到返回-1

includes:

let str = "asdfasdfasdfasdf"

console.log(str.includes('apple'));  //返回true/false

检测字符串以什么开头、结尾:

str.startsWidth(要检测的东西);

str.endsWidth(要检测的东西); //一般用于文件上传以什么结尾(png,jpg等)

让字符串重复多少次:

str.repeat(次数)

你可能感兴趣的:(ES6常用语法(解构、字符串模板、字符串操作))