js中点击返回顶部

1
2
3
handleScrollTop(){
window.scrollTo(0, 0);
}

javascript中new url()属性,轻松解析url地址

首先写一个假的地址(q=URLUtils.searchParams&topic=api)相当于当前的window.location.href

1
2
3
4
5
6
7
8
9
10
11
const urlParams = new URL(window.location.href);
urlParams.searchParams.has("topic") === true; // true
urlParams.searchParams.get("topic") === "api"; // true
urlParams.searchParams.getAll("topic"); // ["api"]
urlParams.searchParams.get("foo") === ""; // true
urlParams.searchParams.append("topic", "webdev");
urlParams.searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev"
urlParams.searchParams.set("topic", "More webdev");
urlParams.searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev"
urlParams.searchParams.delete("topic");
urlParams.searchParams.toString(); // "q=URLUtils.searchParams"

js字符串数组['1','2','3']转number

1
2
let arr = ['1','2','3'];
arr.split(',').map(Number);

数组常用方法

扩展运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let arr = [1, 2, 3];
let arr2 = [...arr]; //等同于 let arr2 = Array.from(arr);

let [a, b, c] = [1, 2, 3]; //a=1, b=2, c=3

let [ , , third] = ["foo", "bar", "baz"]; //third="baz"

let [head, ...tail] = [1, 2, 3, 4]; //head=1, tail=234

let [x, y, ...z] = ['a']; //x=a, y=undefined, z=[], 如果解构不成功,变量的值就等于undefined

let [x, y = 'b'] = ['a']; // x='a', y='b',解构赋值允许指定默

// 解构赋值超强
const {
form: { validateFields }
} = this;
validateFields((err, values) => {
if (!err) {
// eslint-disable-next-line no-console
console.log("Received values of form: ", values);
}
});
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×