李晓爽*_*李晓爽 3 javascript class this
class Obj {
static log(){
console.log(this)
}
}
var test = Obj.log
Obj.log() // class Obj
test() // undefinedRun Code Online (Sandbox Code Playgroud)
这是我运行的代码没有 'use strict'.结果我不明白.为什么函数会test记录undefined而不是全局对象?
这是因为class代码总是严格的代码.从规格
注意类定义始终是严格的代码.
由于class代码始终是严格的,因此该方法log是一个严格的函数(因此,其内部[[ThisMode]]插槽设置为strict),当您调用未设置的严格函数时this,this将设置undefined为调用.
规范中的操作链是通过最终在thisValue设置为的情况下调用规范的抽象EvaluateDirectCall操作来评估直接调用表达式.这反过来又调用呼叫沿传递thisValue,它调用[CALL]]上传递函数thisValue为thisArgument,这就要求OrdinaryCallBindThis,检查函数的[[ThisMode],如果是,设置thisValue到thisArgument的情况下直接进一步的逻辑(如果它不严格,它将替换和使用全局对象,并在任何其他值上执行ToObject).undefinedstrictnullundefined
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |