javascript - 类中嵌套函数之间的对象上下文('this')
问题描述
希望你们都做得很好。
我有一个关于对象的上下文如何在 Node.JS(或一般的 JS)上工作的问题。我理解“当您在 Javascript 中调用顶级函数时,函数内的 this 关键字引用默认对象”。例如,我有以下功能:
function nestedFunctions() {
var a = "I am a variable"
console.log("Initial context:")
console.log(this)
function one() {
console.log("First nested function, this is my this context:")
console.log(this)
console.log("First nested function, this is my a value:")
console.log(a)
function two() {
console.log("Second nested function, this is my this context:")
console.log(this)
console.log("Second nested function, this is my a value:")
console.log(a)
}
two()
}
one()
}
nestedFunctions()
给出以下输出:
Initial context:
Object Global
First nested function, this is my this context:
Object Global
First nested function, this is my a value:
I am a variable
Second nested function, this is my this context:
Object Global
Second nested function, this is my a value:
I am a variable
到目前为止,一切正常,函数获取了全局上下文。当我尝试时:
var rex = {
nestedFunctions() {
var a = "I am a variable"
console.log("Initial context:")
console.log(this)
function one() {
console.log("First nested function, this is my this context:")
console.log(this)
console.log("First nested function, this is my a value:")
console.log(a)
function two() {
console.log("Second nested function, this is my this context:")
console.log(this)
console.log("Second nested function, this is my a value:")
console.log(a)
}
two()
}
one()
}
}
rex.nestedFunctions()
输出是:
Initial context:
{ nestedFunctions: [Function: nestedFunctions] }
First nested function, this is my this context:
Object Global
First nested function, this is my a value:
I am a variable
Second nested function, this is my this context:
Object Global
Second nested function, this is my a value:
I am a variable
到目前为止一切都很好。父函数获得 "this" = { nestedFunctions: [Function: nestedFunctions] } 因为是对象上下文,而嵌套函数获得全局上下文,因为它们没有绑定到父上下文。
但我的疑问是在下一个案例中:
class Dog {
constructor(name) {
this.name = name
}
bark() {
console.log(`Woof, my name is ${this.name}`);
}
nestedFunctions() {
var a = "I am a variable"
console.log("Initial context:")
console.log(this)
function one() {
console.log("First nested function, this is my this context:")
console.log(this)
console.log("First nested function, this is my a value:")
console.log(a)
function two() {
console.log("Second nested function, this is my this context:")
console.log(this)
console.log("Second nested function, this is my a value:")
console.log(a)
}
two()
}
one()
}
}
let rex = new Dog('Rex')
rex.nestedFunctions()
输出在哪里:
Initial context:
Dog { name: 'Rex' }
First nested function, this is my this context:
undefined
First nested function, this is my a value:
I am a variable
Second nested function, this is my this context:
undefined
Second nested function, this is my a value:
I am a variable
为什么嵌套函数上的“this”上下文在这种情况下是“未定义”而不是全局上下文,就像前面的例子一样?为什么在处理类时默认上下文是未定义的(而不是“全局”)?
解决方案
这是因为 JavaScript 的主体class
在严格模式下执行。顾名思义,这种模式限制性更强。在这种执行模式的特点中,this
不将window
被调用的函数内部称为裸函数,而是返回undefined
。
推荐阅读
- php - 无法使用 PHP 从 YouTube Data API v3 获取 ytRating
- reactjs - react-native 包本身抛出 babel 错误
- json - 存在用于验证列表之一中的至少一个元素的 Jsonschema
- python - 有没有办法生成 Matplotlib 动画帧以响应事件?
- angular - 我可以在 Angular SPA 中导航到任意 html 而不创建组件吗?
- java - Spring Boot Callable - 401 Unauthorized: [no body]
- git - Azure Databricks - 从笔记本克隆 git 存储库
- python - 蚂蚁广告 discord.py
- json - 在出现的情况下打印(并排除)两个模式之间的模式
- python - 通过时间查找每个项目和 ID 的平均值(Python)