JavaScript中的this
this 和切换执行上下文有关,取决于函数执行时刻,调用函数时所使用的引用。
8 种函数
// 普通函数
function(){
...
}
// 箭头函数
() => {
...
}
// 生成器函数
function* (){
...
}
// 类中的函数
class A{
function(){
...
}
}
// 类
class B{
constructor(){
...
}
}
// 异步的普通函数
async function(){
...
}
// 异步的箭头函数
async () => {
...
}
// 异步的生成器函数
async function* (){
...
}
this 的机制
- [[thisMode]]私有属性:
- global:当 this 为 undefined 时,取全局对象(普通函数)
- lexical:从上下文中取值(箭头函数)
- strict:严格按照调用取值,可能为 null 或 undefined(class 或严格模式)
- [[thisBindingStatus]]私有属性:当函数创建新的上下文环境(例如函数各种调用方式)时,会根据新的[[thisMode]] 来标记[[thisBindingStatus]]
- 类似于链表结构
本文来自博客园,作者:jinzhepro,转载请注明原文链接:https://www.cnblogs.com/jinzhepro/p/19233891

浙公网安备 33010602011771号