...
...
...
...
...
...
...
...
...
...
...
...
...
// set 数据结构(类数组,无数值下标)
let setArr = new Set ()
// 添加值
setArr.add('a')
setArr.add('b')
setArr.add('c')
// 以上可以写成
let setArr = new Set ().add('a').add('b').add('c')
console.log(setArr)
// 删除值
setArr.delete('b')
console.log(setArr)
// 判断该值是否存在
console.log(setArr.has('a')) // true
console.log(setArr.has('b')) // false
// 输出元素个数
console.log(setArr.size) // 2
// 清空元素
setArr.clear()
// 循环遍历数据
for(let item of setArr){
console.log(item)
} // a , c 默认遍历values
// 循环遍历数据
for(let item of setArr.values()){
console.log(item)
} // a , c
// 循环遍历下标
for(let item of setArr.keys()){
console.log(item)
} // a , c
// 循环遍历
for(let item of setArr.entries()){
console.log(item)
} // ["a", "a"] , ["c", "c"]
// 循环遍历
setArr.forEach((value,index) => {
console.log(value,index)
}) // a a , c c
// 存放数组
let set = new Set()
let json = {
a: 1,
b: 2,
c: 3
}
let json2 = {
a: 1,
b: 2,
c: 3
}
set.add(json).add(json2)
set.forEach((item) => console.log(item)) // {a: 1, b: 2, c: 3} , {a: 1, b: 2, c: 3}
用途:
// 数组去重
let arr = [1, 2, 3, 4, 6, 7, 9, 8, 5, 4, 6, 7, 8, 4, 5, 6, 2]
let newArr = [...new Set(arr)]
console.log(newArr) // [1, 2, 3, 4, 6, 7, 9, 8, 5]
// 映射新数组
let set = new Set([1, 2, 3, 4, 5])
set = new Set([...set].map(val => val * 2))
console.log(set) // Set(5) {2, 4, 6, 8, 10}
// 过滤
let set = new Set([1, 2, 3, 4, 5])
set = new Set([...set].filter(val => val % 2 == 0))
console.log(set) // Set(2) {2, 4}
// 存放数组
let wSet = new WeakSet()
let json = {
a: 1,
b: 2,
c: 3
}
let json2 = {
a: 1,
b: 2,
c: 3
}
set.add(json).add(json2)
console.log(wSet) // WeakSet {{…}, {…}}
总结:
// 新建Map
let map = new Map ()
let json = {
name: 'zephyr',
age: 22
}
// 设置值
map.set('a',"aaa")
map.set(json,"aaa")
map.set('aaa',json)
// 输出值
console.log(map) // Map(3) {"a" => "aaa", {…} => "aaa", "aaa" => {…}}
// 删除值
map.delete('aaa')
// 获取值
console.log(map.get('aaa')) // undefined
// 判断值
console.log(map.has('aaa')) // false
// 清空Map
map.clear()
console.log(map) // Map(0) {}
// for...of循环Map
for(let [k,v] of map){
console.log(k,v)
} // a aaa , {name: "zephyr", age: 22} "aaa" , aaa {name: "zephyr", age: 22}
// forEach循环Map
map.forEach((k,v) => console.log(k,v)) //aaa a , aaa {name: "zephyr", age: 22} , {name: "zephyr", age: 22} "aaa"
WeakMap 的 key 只能是 Object 类型。 原始数据类型是不能作为 key 的(比如 Symbol)。
var wm1 = new WeakMap(),
wm2 = new WeakMap(),
wm3 = new WeakMap();
var o1 = {},
o2 = function(){},
o3 = window;
wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // value可以是任意值,包括一个对象
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // 键和值可以是任意对象,甚至另外一个WeakMap对象
wm1.get(o2); // "azerty"
wm2.get(o2); // undefined,wm2中没有o2这个键
wm2.get(o3); // undefined,值就是undefined
wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true (即使值是undefined)
wm3.set(o1, 37);
wm3.get(o1); // 37
wm3.clear();
wm3.get(o1); // undefined,wm3已被清空
wm1.has(o1); // true
wm1.delete(o1);
wm1.has(o1); // false
总结:
Set 里面是数组,没有重复值,没有key,没有get方法;
Map对json功能增强,key可以是任意类型值。
// 二进制 (binary)
let a = 0b010101
console.log(a) // 21
// 八进制 (octal)
let b = 0o666
console.log(b) // 438
// 判断NaN
Number.isNaN(NaN) --> true
// 判断Number
Number.isFinite(1.5) --> true
Number.isFinite(true) --> false
// 判断整数
Number.isInteger(1) --> true
Number.isInteger(1.5) --> false
js安全整数: [ -(2^53-1),(2^53-1)] ,此范围外计算将丢失精度。
let a = 2 ** 53
console.log(Number.isSafeInteger(a)) // false
console.log(Number.isSafeInteger(a - 1)) // true
let a = 2 ** 53 - 1
console.log(a) // 9007199254740991
console.log(Number.MAX_SAFE_INTEGER) // 9007199254740991
Math.abs(),Math.sqrt(),Math.sin()...
// 截取,只保留整数部分
Math.trunc(1.1) // 1
Math.trunc(1.9) // 1
// 判断正负
Math.sign(1) // 1
Math.sign(1.5) // 1
Math.sign(-1) // -1
Math.sign(-1.5) // -1
Math.sign(0) // 0
Math.sign(-0) // -0
Math.sign('aaa') // NaN
// 计算立方根
Math.cbrt(27) // 3
// 语法 (?<name>)
let str = '2019-03-30'
let reg = /(\d{4})-(\d{2})-(\d{2})/
console.log(str.match(reg)) // ["2019-03-30", "2019", "03", "30", index: 0, input: "2019-03-30", groups: undefined]
// 使用命名捕获
let str = '2019-03-30'
let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
let dateArr = str.match(reg).groups
console.log(dateArr) // { year: "2019", month: "03", day: "30" }
let { year, month, day} = str.match(reg).groups
console.log(year, month, day) // 2019 03 30
// 普通反向引用
\1 \2 $1 $2
// 反向引用命名捕获语法 (\<name>)
let reg = /^(?<zephyr>welcome)-\k<zephyr>$/
let str = 'a-a'
let str2 = 'zephyr-zephyr'
let str3 = 'welcome-welcome'
console.table(reg.test(str),reg.test(str3),reg.test(str3)) // false true true
Proxy是设计模式之一(代理模式),常用于扩展(增强)对象的一些功能。
例如VUE中的:Vue.config.keyCodes.enter = 65;
...
new Proxy (target, handler) // 被代理的对象,对代理对象进行的操作
// 实例
let obj = {
name: 'zephyr'
}
console.log(obj.name)
let newObj = new Proxy(obj, {
get(targrt, property) {
// console.log(targrt, property)
console.log(`你访问了${property}属性!`)
return targrt[property]
}
})
newObj.name // zephyr , 你访问了name属性!
实现访问一个对象身上的属性,默认不存在时是undefined,现在要求修不存在时抛出错误(警告)信息。
let obj = {
name: 'zephyr'
}
console.log(obj.name)
let newObj = new Proxy(obj, {
get(targrt, property) {
return targrt[property]
}
})
console.log(newObj.name) // zephyr
console.log(newObj.age) // undefined
// 自定义捕获错误并显示提示消息
let obj = {
name: 'zephyr'
}
console.log(obj.name)
let newObj = new Proxy(obj, {
get(targrt, property) {
if (property in targrt) {
return targrt[property]
} else {
throw new ReferenceError(`${property}属性不在此对象上!`)
}
}
})
newObj.name // zephyr
newObj.age // Uncaught ReferenceError: age属性不在此对象上!
...
const hide2 = (el) => Array.from(el).forEach(e => (e.style.display = 'none'));
// 事例:隐藏页面上所有`<img>`元素?
hide(document.querySelectorAll('img'))
页面DOM里的每个节点上都有一个classList
对象,程序员可以使用里面的方法新增、删除、修改节点上的CSS类。使用classList
,程序员还可以用它来判断某个节点是否被赋予了某个CSS类。
const hasClass = (el, className) => el.classList.contains(className)
// 事例
hasClass(document.querySelector('p.special'), 'special') // true
const toggleClass = (el, className) => el.classList.toggle(className)
// 事例 移除 p 具有类`special`的 special 类
toggleClass(document.querySelector('p.special'), 'special')
const getScrollPosition = (el = window) => ({
x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
});
// 事例
getScrollPosition(); // {x: 0, y: 200}
const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
}
// 事例
scrollToTop()
window.requestAnimationFrame()
告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。该方法需要传入一个回调函数作为参数,该回调函数会在浏览器下一次重绘之前执行。
requestAnimationFrame
:优势:由系统决定回调函数的执行时机。60Hz的刷新频率,那么每次刷新的间隔中会执行一次回调函数,不会引起丢帧,不会卡顿。
const elementContains = (parent, child) => parent !== child && parent.contains(child);
// 事例
elementContains(document.querySelector('head'), document.querySelector('title'));
// true
elementContains(document.querySelector('body'), document.querySelector('body'));
// false
const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
const { top, left, bottom, right } = el.getBoundingClientRect();
const { innerHeight, innerWidth } = window;
return partiallyVisible
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
};
// 事例
elementIsVisibleInViewport(el); // 需要左右可见
elementIsVisibleInViewport(el, true); // 需要全屏(上下左右)可以见
const getImages = (el, includeDuplicates = false) => {
const images = [...el.getElementsByTagName('img')].map(img => img.getAttribute('src'));
return includeDuplicates ? images : [...new Set(images)];
};
// 事例:includeDuplicates 为 true 表示需要排除重复元素
getImages(document, true); // ['image1.jpg', 'image2.png', 'image1.png', '...']
getImages(document, false); // ['image1.jpg', 'image2.png', '...']
const detectDeviceType = () =>
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
? 'Mobile'
: 'Desktop';
// 事例
detectDeviceType(); // "Mobile" or "Desktop"
const currentURL = () => window.location.href
// 事例
currentURL() // 'https://google.com'
const getURLParameters = url =>
(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
{}
);
// 事例
getURLParameters('http://url.com/page?n=Adam&s=Smith'); // {n: 'Adam', s: 'Smith'}
getURLParameters('google.com'); // {}
const formToObject = form =>
Array.from(new FormData(form)).reduce(
(acc, [key, value]) => ({
...acc,
[key]: value
}),
{}
);
// 事例
formToObject(document.querySelector('#form'));
// { email: 'test@email.com', name: 'Test Name' }
const get = (from, ...selectors) =>
[...selectors].map(s =>
s
.replace(/\[([^\[\]]*)\]/g, '.$1.')
.split('.')
.filter(t => t !== '')
.reduce((prev, cur) => prev && prev[cur], from)
);
const obj = { selector: { to: { val: 'val to select' } }, target: [1, 2, { a: 'test' }] };
// Example
get(obj, 'selector.to.val', 'target[0]', 'target[2].a');
// ['val to select', 1, 'test']
const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
delay(
function(text) {
console.log(text);
},
1000,
'later'
);
// 1秒后打印 'later'
const triggerEvent = (el, eventType, detail) =>
el.dispatchEvent(new CustomEvent(eventType, { detail }));
// 事例
triggerEvent(document.getElementById('myId'), 'click');
triggerEvent(document.getElementById('myId'), 'click', { username: 'bob' });
自定义事件的函数有 Event
、CustomEvent
和 dispatchEvent
// 向 window派发一个resize内置事件
window.dispatchEvent(new Event('resize'))
// 直接自定义事件,使用 Event 构造函数:
var event = new Event('build');
var elem = document.querySelector('#id')
// 监听事件
elem.addEventListener('build', function (e) { ... }, false);
// 触发事件.
elem.dispatchEvent(event);
CustomEvent
可以创建一个更高度自定义事件,还可以附带一些数据,具体用法如下:
var myEvent = new CustomEvent(eventname, options);
其中 options 可以是:
{
detail: {
...
},
bubbles: true, //是否冒泡
cancelable: false //是否取消默认事件
}
其中 detail
可以存放一些初始化的信息,可以在触发的时候调用。其他属性就是定义该事件是否具有冒泡等等功能。
内置的事件会由浏览器根据某些操作进行触发,自定义的事件就需要人工触发。
dispatchEvent
函数就是用来触发某个事件:
element.dispatchEvent(customEvent);
上面代码表示,在 element
上面触发 customEvent
这个事件。
// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) });
// create and dispatch the event
var event = new CustomEvent("cat", {"detail":{"hazcheeseburger":true}});
obj.dispatchEvent(event);
使用自定义事件需要注意兼容性问题,而使用 jQuery 就简单多了:
// 绑定自定义事件
$(element).on('myCustomEvent', function(){});
// 触发事件
$(element).trigger('myCustomEvent');
// 此外,你还可以在触发自定义事件时传递更多参数信息:
$( "p" ).on( "myCustomEvent", function( event, myName ) {
$( this ).text( myName + ", hi there!" );
});
$( "button" ).click(function () {
$( "p" ).trigger( "myCustomEvent", [ "John" ] );
});
const off = (el, evt, fn, opts = false) => el.removeEventListener(evt, fn, opts);
const fn = () => console.log('!');
document.body.addEventListener('click', fn);
off(document.body, 'click', fn);
const formatDuration = ms => {
if (ms < 0) ms = -ms;
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
second: Math.floor(ms / 1000) % 60,
millisecond: Math.floor(ms) % 1000
};
return Object.entries(time)
.filter(val => val[1] !== 0)
.map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
.join(', ');
};
// 事例
formatDuration(1001); // '1 second, 1 millisecond'
formatDuration(34325055574);
// '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'
const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>
(dateFinal - dateInitial) / (1000 * 3600 * 24);
// 事例
getDaysDiffBetweenDates(new Date('2017-12-13'), new Date('2017-12-22')); // 9
const httpGet = (url, callback, err = console.error) => {
const request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = () => callback(request.responseText);
request.onerror = () => err(request);
request.send();
};
httpGet(
'https://jsonplaceholder.typicode.com/posts/1',
console.log
);
// {"userId": 1, "id": 1, "title": "sample title", "body": "my text"}
const httpPost = (url, data, callback, err = console.error) => {
const request = new XMLHttpRequest();
request.open('POST', url, true);
request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
request.onload = () => callback(request.responseText);
request.onerror = () => err(request);
request.send(data);
};
const newPost = {
userId: 1,
id: 1337,
title: 'Foo',
body: 'bar bar bar'
};
const data = JSON.stringify(newPost);
httpPost(
'https://jsonplaceholder.typicode.com/posts',
data,
console.log
);
// {"userId": 1, "id": 1337, "title": "Foo", "body": "bar bar bar"}
const counter = (selector, start, end, step = 1, duration = 2000) => {
let current = start,
_step = (end - start) * step < 0 ? -step : step,
timer = setInterval(() => {
current += _step;
document.querySelector(selector).innerHTML = current;
if (current >= end) document.querySelector(selector).innerHTML = end;
if (current >= end) clearInterval(timer);
}, Math.abs(Math.floor(duration / (end - start))));
return timer;
};
// 事例
counter('#my-id', 1, 1000, 5, 2000);
// 让 `id=“my-id”`的元素创建一个2秒计时器
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
// 事例
copyToClipboard('Lorem ipsum');
// 'Lorem ipsum' copied to clipboard
const isBrowserTabFocused = () => !document.hidden;
// 事例
isBrowserTabFocused(); // true
const fs = require('fs');
const createDirIfNotExists = dir => (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined);
// 事例
createDirIfNotExists('test');