let url = location.protocol + '//baidu.com/query?q=' + word + '&qn=' + queryWord;
let url = `${location.protocol}//baidu.com/query?q=${word}&qn=${queryWord}`;
let m = 'test';
let n = `m+:${m}`;
n //"m+:test"
m //"test"
m += 'noTest' //改变字符串模板中的 m 的值
m //"testnoTest"
n //"m+:test"
let k = `one line two line 3 line`; k //输出如下 "one line two line 3 line"
test:${1+1} //"test2"
`TEST:${new Date()}` //"TEST:Wed Sep 27 2017 15:48:53 GMT+0800 (CST)"
var tt = {a:1,b:2};
`test:${tt}` //"test:[object Object]"
tt.toString() //"[object Object]"
// 完全匹配模式
let [g, h, l] = ['gg', 'hh', 'll'];
g //"gg"
h //"hh"
l //"ll"
//部分匹配、设置初始值 【初始值需要对象或者数组的成员值严格等于undefined才会生效】
let [v, , x, z='zzz'] = ['vv', 'bb', 'xxxx'];
v //"vv"
x //"xxxx"
z // "zzz"
//对象解构 【注意对象解构时冒号 : 前面的是模式,用来和对象的键名匹配,不是变量】
let {a:aaa,b,c=9,d:ddd=9,e:eee=9,f} = {a:1,b:2,c:3,d:4}
aaa // 1
b //2
c //3
ddd //4
eee //9
f // undefined
//字面量的对象解构需要加上括号
({a, b} = {a:1, b:2})
({a, b, ...rest} = {a:1, b:2, c:3, d:4});
let a =1, b=2; [a, b] = [b, a];
function f() {
return [1, 2, 3];
}
let [a, , b] = f();
//就拿navigator.userAgent来说,想要获取不同的内容,类似如下: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" let ua = /(Mac\s?OS\s?X\s?)([\d_]+).*Chrome\/([\d.]+)/.exec(navigator.userAgent); /* ua = ["Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100", "Mac OS X ", "10_12_6", "61.0.3163.100"] */ let [ , ,macVersion, chromeVersion] = ua; //分别获取mac的系统版本和chrome的版本
//数组字面量扩展
let test = [3,4,5],
copyTest = [...test], // copyTest [3,4,5]
extTest = [1,2,...test]; //extTest [1,2,3,4,5]
//对象字面量扩展
let obj = {a: 1, b: 2},
copyObj = {...obj}, //
extObj = {...obj, c: 3};
function myFunction(x, y, z) {
return x+y+z;
}
var args = [0, 1, 2];
myFunction(...args); //3
Array.prototype.push.apply(arr) //=>// Array.prototype.push(...arr)
let array1 = [1,2,3], array2 = [4,5,6],array3 = [...array1, ...array2];
function sortRestArgs(...theArgs) {
var sortedArgs = theArgs.sort();
return sortedArgs;
}
alert(sortRestArgs(5,3,7,1)); // 弹出 1,3,5,7
var [a, ...rest] = [1, 2, 3, 4]; console.log(a);//1 console.log(rest);//[2, 3, 4]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有