变量的解构赋值
// 数组的解构赋值
let [a,b,c] = [1,2,3]; //1,2,3
let [a,b,c] = [,123,]; //undefined 123 undefined
let [a=111,b,c] = [,123,]; //111 123 undefined
console.log(a,b,c);
对象的解构赋值
let {foo,bar} = {foo : 'hello',bar : 'hi'};//hello hi
let {foo,bar} = {bar : 'hi',foo : 'hello'};//hello hi
console.log(foo,bar);
===================== let {cos,sin,random} = Math; console.log(typeof cos);//function
console.log(typeof sin);//function
console.log(typeof random);//function
对象属性别名(如果有了别名,那么原来的名字就无效了)
let {foo,bar} = {bar : 'hi',foo : 'nihao'};
console.log(foo,bar) //nihao hi
================================================================
let {foo:abc,bar} = {bar : 'hi',foo : 'nihao'};
console.log(abc,bar)//nihao hi
console.log(foo,bar); // foo is not defined
对象的解构赋值指定默认值
let {foo:abc='hello',bar} = {bar : 'hi'};
console.log(abc,bar);//hello hi
字符串的解构赋值
let [a,b,c,d,e,length] = "hello";
console.log(a,b,c,d,e);//h e l l o
console.log(length);// undefined let {length} = "hi";
console.log(length);//2
/*
字符串相关扩展
includes() 判断字符串中是否包含指定的字串(有的话返回true,否则返回false)
参数一:匹配的字串;参数二:从第几个开始匹配
startsWith() 判断字符串是否以特定的字串开始
endsWith() 判断字符串是否以特定的字串结束 模板字符串
*/
console.log('hello world'.includes('world',7));//false
console.log('hello world'.includes('world',6));//true let url = 'admin/login.html';
console.log(url.startsWith('admin'));//true
console.log(url.endsWith('html'));//true 反引号表示模板,模板中的内容可以有格式,通过${}方式填充数据
let obj = {
username : 'lisi',
age : '12',
gender : 'male'
} let fn = function(info){
return info;
}
let tpl = `
<div>
<span>${obj.username}</span>
<span>${obj.age}</span>
<span>${obj.gender}</span>
<span>${1+1}</span>
<span>${fn('nihao')}</span>
</div>
`;
console.log(tpl); <div>
<span>lisi</span>
<span>12</span>
<span>male</span>
<span>2</span>
<span>nihao</span>
</div>