-
函数作为返回值
function create() { const a = 100; return function () { consolo.log(a); } } const fn = create(); const a = 200; fn() // 100
-
函数作为参数被传递
function print(fn) { const a = 200; fn(); } const a = 100; function fn() { consolo.log(a); } // 自由变量的查找,是在函数定义的地方,向上级作用域查找 // 不是在执行的地方
-
this的不同场景,如何取值
- 当做普通函数被调用时,this取window。
- 使用call、apply、bind,this是传入的值。
- 做为对象方法调用,this就是对象方法本身。
- 在class的方法中调用,this是实例本身。
- 在箭头函数中,this是上一级this作用域的值。
-
手写bind
Function.prototype.bind1 = function () { // 将参数拆解为数组 // const args = Array.prototype.slice.call(arguments); const args = [...arguments]; // 获取 this (数组的第一项) const t = args.shift(); // 返回一个函数 return () => { return this.apply(t, args); } }
JS 面试题常见知识点(三)—— 闭包
最新推荐文章于 2024-05-12 17:57:45 发布