1.获取url里面的参数值或者追加查询字符串
假如我们有这样一个url,"?post=1234&action=edit",可以这样处理这个url
var urlParams = new URLSearchParams(‘?post=1234&action=edit‘); console.log(urlParams.has(‘post‘)); console.log(urlParams.get(‘action‘)); // "edit" console.log(urlParams.getAll(‘action‘)); // ["edit"] console.log(urlParams.toString()); // "?post=1234&action=edit" console.log(urlParams.append(‘active‘, ‘1‘)); // "?post=1234&action=edit&active=1"
2.函数直接赋值一个函数,如果没有传参,我们就直接抛出错误提醒,如果一个组件里面有很多方法,我们就可以直接复用,而不用每个方法里面都去做非空判断处理
const isRequired = () => { throw new Error(‘param is required‘); }; const hello = (name = isRequired()) => { console.log(`hello ${name}`) }; // 抛出一个错误,因为没有参数没有传 hello(); // 没有问题 hello(‘hello‘)
3.document.scrollingElement控制窗体滚动高度,一统江湖
(document.documentElement.scrollTop PC端) (document.body.scrollTop 移动端)
希望页面滚动定位到具体位置的时候,如400像素,直接一行代码就可以搞定了:document.scrollingElement.scrollTop = 400;
原文:https://www.cnblogs.com/musi03/p/10794223.html