liuqinh2s' blog

Do something cool!


  • 首页

  • 关于

  • 标签

  • 归档

  • 前端面试

  • 新闻

  • 收藏

  • 导航

  • 搜索

知不知道最新的url参数获取的API?

发表于 2022-07-26 | 更新于: 2025-09-30
字数统计: 254
核心 API:URLSearchParams现代浏览器原生支持的查询参数处理标准接口 基础使用示例: 123456// 从当前URL获取参数const params = new URLSearchParams(window.location.search);// 获取单个参数值const name ...
阅读全文 »

扁平数组嵌套化(flat to nest)

发表于 2022-07-25 | 更新于: 2025-09-30
字数统计: 243

扁平数组嵌套化(flat to nest)

  1. 菜单数组转换为嵌套树形结构,但示例只有两级
1
2
3
4
5
6
7
8
9
10
11
12
[
{ id: 1, menu: '水果', level: 1 },
{ id: 2, menu: '橘子', level: 2, parentId: 1 }
// ...
]
// 转换为
[
{
id: 1, menu: '水果', level: 1, children: [{ id: 2, menu: '橘子', level: 2, parentId: 1 }]
},
// ...
]

以下是我给出的解答,未经过严格测试

阅读全文 »

寻找字符串中出现最多的字符怎么实现?

发表于 2022-07-24 | 更新于: 2025-09-30
字数统计: 127

寻找字符串中出现最多的字符怎么实现?

遍历一遍字符串,用个 map 记录每个字符出现的次数,然后遍历一遍 map,取出出现次数最多的字符

阅读全文 »

JavaScript执行上下文之作用域链

发表于 2022-07-14 | 更新于: 2025-09-30
字数统计: 573

执行上下文中有个作用域链,当查找一个变量时会顺着这个链找。

函数的作用域在函数定义的时候就决定了。这是因为函数对象有个内部属性[[scope]]

函数的生命周期分为:函数创建 和 函数调用。

函数创建的时候,会把其所处执行上下文的作用域链直接赋值给函数的内部属性[[scope]](这就是词法作用域的原理了),函数调用的时候会创建自己的执行上下文,并把自己的AO和[[scope]]合并成新的作用域链:

假设要实现动态作用域的话,[[scope]]就得在调用时去执行上下文栈的上一帧去取。

1
Scope = [AO].concat([[scope]]);
阅读全文 »

JavaScript 闭包

发表于 2022-07-14 | 更新于: 2025-09-30
字数统计: 1,148

闭包是干什么用的

本质上闭包就是为了拓展查找自由变量的范围

MDN 对闭包的定义为:

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment)

一个函数和对其周围状态(lexical environment,词法环境)的引用捆绑在一起(或者说函数被引用包围),这样的组合就是闭包(closure)。

1
2
3
4
5
6
7
8
9
function init() {
var name = 'Mozilla'; // name is a local variable created by init
function displayName() {
// displayName() is the inner function, a closure
console.log(name); // use variable declared in the parent function
}
displayName();
}
init();

name 是 displayName 函数所处环境中的变量,它们一起构成了闭包。而闭包的实现依赖于执行上下文中的作用域链。

上面这个例子有点平平无奇了,让我们看一个神奇一点的例子:

阅读全文 »
1…111213…23
liuqinh2s

liuqinh2s

112 日志
49 标签
RSS
GitHub Twitter
Links
  • liam
  • jiyanggg
  • 曾小乱 – 在描绘他的生活倒影
© 2025 liuqinh2s | Site words total count: 124.0k
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4