解构赋值:
解构赋值:
* 非常有用,特别在做数据交互 ajax
let [a,b,c] =[12,5, 6];
//a=12 b=5 c=6
//注意: 左右两边,结构格式要保持一致
json:
let {name,age, job} = {
name:'Strive',
age:18,
job:'码畜'
};
let {name:n,age:g, job:a} = json;
console.log(n,g,a);
//Strive 18 码畜
//解构时候可以给默认值:
let [a,b, c="默认值"] = ['aaa','bbb'];
let a = 12;
let b = 5;
console.log(a,b,c);//c的值保持不变
//若c无默认值则会显示undefined 若设置默认值为null则可显示null出来
import {a,b,c} from './mod'
let a;
({a} = {a:'apple', b:'banana'});
console.log(a);//apple
扩展运算符
基本用法// [1,2,3,4,5] ...[1,2,3,4,5] -> 1,2,3,4,5
//1,2,3,4,5 -> ... a -> [1, 2, 3, 4, 5]
function show(...a){
console.log(a);
}
show(1,2,3,4,5);//得到数组a
排序用法
function show(...a){
return a.sort();
}
console.log(show(1,9,8,3,2));
传递多个参数
function show(a,b,c){
console.log(a,b,c);
}
show(...[1,9,8]);
复制数组 得到两个相同的数组
let arr =[1,2,3,4,5];
//let arr2 =[...arr];
let arr2 = Array.from(arr);
console.log(arr, arr2);
分割字符串为数组
let str = 'a-b-c';
let arr = Array.from(str);
console.log(arr);//["a", "-", "b", "-", "c"]
字符串查找与字符串模板
include 查找字符串是否存在某个部分 返回结果为布尔值let str = 'apple banana2 pear';
alert(str.includes('banana'));//true
关于字符串一些东西:
字符串查找:
str.indexOf
(要找的东西) 返回索引(位置) ,没找到返回-1
str.includes
(要找的东西) 返回值 true/false
判断浏览器: includes
http://www.xxx.xx
字符串是否以谁开头:
str.startsWith
(检测东西)
返回布尔值
检测地址
字符串是否以谁结尾:
str.endsWith
(检测东西)
返回布尔值
.png
重复字符串 类似复制:
str.repeat
(次数);
填充字符串:
str.padStart
(整个字符串长度, 填充东西) 往前填充
str.padEnd
(整个字符串长度, 填充东西) 往后填充
str.padStart(str.length+padStr.length, padStr)
模板字符串
`` 字符串模板:
优点: 可以随意换行 拼接变量是非常方便!!
不用加来加去!!
${变量名字}
let name ='Strive';
let age = 18;
let str = `这个人叫${name}, 年龄是 ${age}岁`;