Skip to content

Instantly share code, notes, and snippets.

View jacob-lcs's full-sized avatar
❤️
coding with heart

Jacob jacob-lcs

❤️
coding with heart
View GitHub Profile
@jacob-lcs
jacob-lcs / localstorage.js
Last active January 2, 2021 10:24
对 localstorage 进行封装,使用方法见 comment
let storage = window.localStorage;
/**
* 判断是否为 JSON 对象
* @param obj
* @returns {boolean}
*/
function isJSON(obj) {
obj = JSON.stringify(obj);
return /^\{[\s\S]*\}$/.test(obj);
@jacob-lcs
jacob-lcs / supportWebP.js
Last active January 30, 2021 12:11
判断当前宿主环境是否支持 webp
// 方法一,非异步方式
const supportWebP = (function () {
var canvas = typeof document === 'object' ? document.createElement('canvas') : {}
canvas.width = canvas.height = 1
return canvas.toDataURL ? canvas.toDataURL('image/webp').indexOf('image/webp') === 5 : false
})()
// 异步方式获取,比较适合系统中存在一个系统变量来保存结果
window.isSupportWebp = false; // 是否支持
(function() {
@jacob-lcs
jacob-lcs / PriorityQueue.js
Created April 23, 2022 14:05
Java 优先级队列实现
class PriorityQueue {
#top = 0;
#heap;
#comparator;
constructor(comparator = (a, b) => a > b) {
this.#heap = [];
this.#comparator = comparator;
}
#parent(i) {
@jacob-lcs
jacob-lcs / useInterval.js
Created May 2, 2022 09:55
setInterval 在 React hooks 中的实现
import { useEffect, useRef } from 'react'
function useInterval(callback, delay) {
const savedCallback = useRef(callback)
// Remember the latest callback if it changes.
useEffect(() => {
savedCallback.current = callback
}, [callback])