学习笔记学习笔记
主站博客
面试
前端
开发工具
Cesium
GitHub
主站博客
面试
前端
开发工具
Cesium
GitHub
  • 面试
  • 算法

    • hash哈希

      • lc-1-简-两数之和
    • top K

      • lc-215-数组中的第K个最大元素
      • lc-347-中-前 K 个高频元素
      • lc-LCR159-简-库存管理 III
    • 二叉树

      • lc-101-简-对称二叉树
      • lc-102-中-二叉树的层序遍历
      • lc-104-简-二叉树的最大深度
      • lc-105-中-从前序与中序遍历序列构造二叉树
      • lc-114-中-二叉树展开为链表
      • lc-226-简-翻转二叉树
      • lc-236-中-二叉树的最近公共祖先
    • 动态规划

      • lc-1277-中-统计全为 1 的正方形子矩阵
      • lc-221-中-最大正方形
      • lc-62-中-不同路径
      • lc-70-简-爬楼梯
      • lc-72-中-编辑距离
      • lc-746-简-使用最小花费爬楼梯
      • 背包-01背包
      • 背包-完全背包
    • 原地哈希

      • lc-33-中-搜索旋转排序数组
      • lc-442-中-数组中重复的数据
      • lc-448-简-找到所有数组中消失的数字
    • 图

      • lc-207-中-课程表
      • lc-997- 简-找到小镇的法官
    • 待分类

      • lc-11-中-盛最多水的容器
      • lc-121-简-买卖股票的最佳时机
      • lc-128-中-最长连续序列
      • lc-136-简-只出现一次的数字
      • lc-139-中-单词拆分
      • lc-152-中-乘积最大子数组
      • lc-20-简-有效的括号
      • lc-200-中-岛屿数量
      • lc-22-中-括号生成
      • lc-279-中-完全平方数
      • lc-31-中-下一个排列
      • lc-322-中-零钱兑换
      • lc-34-中-在排序数组中查找元素的第一个和最后一个位置
      • lc-39-中-组合总和
      • lc-416-中-分割等和子集
      • lc-42-难-接雨水
      • lc-437-中-路径总和 III
      • lc-438-中-找到字符串中所有字母异位词
      • lc-49-中-字母异位词分组
      • lc-53-中-最大子数组和
      • lc-55-中-跳跃游戏
      • lc-56-中-合并区间
      • lc-560-中-和为 K 的子数组
      • lc-647-中-回文子串
      • lc-79-中-单词搜索
    • 排序

      • 归并排序

        • 归并排序
      • 快排
      • 排序+双指针

        • lc- 524-中-通过删除字母匹配到字典里最长单词
        • lc-15-中-三数之和
        • lc-16-中-最接近的三数之和
        • lc-283-简-移动零
        • lc-3-中-无重复字符的最长子串
        • lc-75-中-颜色分类
      • 计数排序
    • 算法工具库js
    • 递归+回溯

      • lc-17-中-电话号码的字母组合
      • lc-77-中-组合
      • LCR-083-中-全排列-不重复
      • LCR-084-中-全排列-重复
    • 链表

      • lc-142-中-环形链表 II
      • lc-148-中-排序链表
      • lc-160-简-相交链表
      • lc-19-中-删除链表的倒数第 N 个结点
      • lc-287-中-寻找重复数
  • 手撕代码

    • 柯里化 Curring
    • h5 Evnet详解
    • promiseify化
    • script标签详解
    • vue-SSR服务端渲染
    • vue双向绑定
    • vue核心原理全解
    • 路由的实现
    • 原型链
    • 变量提升
    • 宏任务、微任务
    • 左右两栏布局
    • 异步
    • 微前端
    • 实现js的filter函数
    • 实现promise
    • 捕获、冒泡
    • js中new的本质
    • Object.xxx
    • this
    • 反射Reflect,、代理Proxy
    • 基础类型
    • 深拷贝-浅拷贝
    • 继承
    • 跨域
    • 闭包
    • 防抖-节流

闭包

参考

  • 深入理解JavaScript闭包之什么是闭包

什么是闭包

  • 当函数可以记住并访问所在的词法作用域时,就产生了闭包,即使函数是在当前词法作用域之外执行的。
  • 闭包是指有权访问另一个函数作用域中变量的函数

常见的一些闭包

function foo(a) {
    setTimeout(function timer(){
        console.log(a)
    }, 1000)
}
foo(2);
for(var i = 0; i < 5; i++) {
    setTimeout(() => {
        console.log(i);
    }, i * 1000);
}

基础闭包代码

for (var i = 0; i < 5; i++) {
  setTimeout(() => {
    console.log(i);
  }, i * 1000);
}
for (var i = 0; i < 5; i++) {
  (function x(n) {
    setTimeout(() => {
      return console.log(n);
    }, n * 1000);
  })(i)
}

hook-useState实现



// let throttleLastFireTimes = []
// function throttle(delay = 500, func) {
//   return (function (index) {
//     return function () {
//       let now = Date.now()
//       if (!throttleLastFireTimes[index] || (now - throttleLastFireTimes[index] > delay)) {
//         throttleLastFireTimes[index] = now
//         func && func()
//       }
//     }
//   })(throttleLastFireTimes.length)
// }

// let throttleRender = throttle(500, render)


let memorizedData = []
var index
function useState(initValue) {
  index++
  let val = memorizedData[index] || initValue
  let setVal = (newVal) => {
    memorizedData[index] = newVal
    // throttleRender()
    render()
  }
  return [val, setVal]
}


function render() {
  index = 0

  const [age, setAge] = useState(18)
  const [price, setPrice] = useState(100)

  console.log(`age: ${age}, price: ${price}`);
  setTimeout(() => {
    setAge(age + 1)
    setPrice(price + 1)
  }, 1000);
}

render()

hook-useEffect


let memorizedData = []
var index
function useState(initValue) {
  index++
  let val = memorizedData[index] || initValue
  let setVal = (newVal) => {
    memorizedData[index] = newVal
    render()
  }
  return [val, setVal]
}


function render() {
  let age = 18
  useEffect(() => {
    console.log(`age: ${age}`);
  }, [age])
}


render()
在 GitHub 上编辑此页
上次更新:
贡献者: 国wei
Prev
跨域
Next
防抖-节流