深入JavaScript Day04 - 闭包、this的四种绑
作者:
望穿秋水小作坊 | 来源:发表于
2022-01-12 14:44 被阅读0次
一、闭包补充
1、闭包是如何导致内存泄漏的?如何解决?
- window【持有】了闭包函数,闭包函数【持有】了父级AO
- 后续不再使用闭包函数了,但是闭包函数持有的父级AO始终不能释放
-【解决方案】将window持有的闭包函数,指向null

image.png
2、借助Chrome的性能分析,来验证闭包的持有对象不释放问题?(思路很重要)

image.png
3、如果闭包持有的父级AO中,仅仅使用name,而未使用age,那么age也会被持有吗?
- age不会被闭包持有
- 从浏览器的bugger模式可以看到

image.png
二、this的四种绑定情况
1、this指向其实是有点难知识点,那么为什么JavaScript开发中需要this呢?
- 有this的代码,将获得更好的灵活性、扩展性
- 比如下面代码,没有使用this,那么逻辑代码将和变量名绑定,耦合度就会很高,也不易扩展

image.png
2、浏览器中,在全局作用域下,this指向谁?
3、从下面代码,对于this有什么感悟?

image.png
- this的绑定和定义的位置(编写的位置)没有关系
- this是运行时被绑定的
4、默认绑定?

image.png
5、隐式绑定?

image.png
6、显示绑定?

image.png
7、new绑定

image.png
8、call、apply、bind的区别?

image.png
9、四种绑定规则的优先级?

image.png
本文标题:深入JavaScript Day04 - 闭包、this的四种绑
本文链接:https://www.haomeiwen.com/subject/wpswcrtx.html
网友评论