利用a标签解析 URL
function parseURL(url) {var a = document.createElement('a');a.href = url;return {host: a.hostname,port: a.port,query: a.search,params: (function(){var ret = {},seg = a.search.replace(/^\?/,'').split('&'),len = seg.length, i = 0, s;for (;i<len;i++) {if (!seg[i]) { continue; }s = seg[i].split('=');ret[s[0]] = s[1];}return ret;})(),hash: a.hash.replace('#','')};}console.log(parseURL(window.location.href));
标记语句
label和continue/break配合使用, 可以实现双重for循环时,满足条件即可直接跳出,避免浪费资源
- break:立刻跳出循环或代码块。
- continue:结束本次循环,继续开始下一次,又可以说是立即终止本次循环,返回循环结构头部,开始下一轮循环
- 标签(label): 相当于定位符,用于跳转到程序的任意位置 .
firstLoop: // label标签,名字自定义for (let i = 0; i < 3; i++) {for (let j = 0; j < 3; j++) {if (i === j) {continue firstLoop; // 继续 从firstLoop位置 循环,// break firstLoop; // 中止 firstLoop 循环}console.log(`i = ${i}, j = ${j}`);}}// 输出i = 1, j = 0i = 2, j = 0i = 2, j = 1// 无labelfor (let i = 0; i < 3; i++) {for (let j = 0; j < 3; j++) {if (i === j) {continue}console.log(`i = ${i}, j = ${j}`);}}// 输出i = 0, j = 1i = 0, j = 2i = 1, j = 0i = 1, j = 2i = 2, j = 0i = 2, j = 1
🌰2:
top:for (var i = 0; i < 3; i++) {for (var t = 0; t < 3; t++) {if (i === 1 && t === 1) {break top;}console.log("i=" + i, "t=" + t);}}// 输出i = 0, t = 0i = 0, t = 1i = 0, t = 2i = 1, t = 0
void运算符
void 运算符对给定的表达式进行求值,然后返回 undefined 由于
void会忽略操作数的值,因此在操作数具有副作用的时候使用void会更加合理。
使用 void 替换 undefined
由于
undefined并不是JavaScript的关键字,所以我们在赋值某个变量为undefined时可能会有点意想不到的结果。
function t(){var undefined = 10;console.log(undefined);}console.log(t()); // 大多数浏览器下都是10
IntersectionObserver
IntersectionObserver 可以用来监听元素是否进入了设备的可视区域之内,而不需要频繁的计算来做这个判断。
IntersectionObserverFun: function() {let self = this;let ele = self.$refs.pride_tab_fixed;if( !!IntersectionObserver ){let observer = new IntersectionObserver(function(){let offsetTop = ele.getBoundingClientRect().top;self.titleFixed = offsetTop < 0;}, {threshold: [1]});observer.observe(document.getElementsByClassName('title_box')[0]);} else {window.addEventListener('scroll', _.throttle(function(){let offsetTop = ele.getBoundingClientRect().top;self.titleFixed = offsetTop < 0;}, 50));}}
