如何解决带有“Console.log()”的执行上下文
据我所知,“执行上下文”是由函数的调用位置决定的——而不是函数声明的位置。
因此在下面的示例中,我希望 this.name
评估为未定义。因此,我不确定 console.log()
如何访问“特许经营”对象内的 this
关键字。
let franchise = {
name: 'How to Train Your Dragon',allMovies: function() {
console.log(this.name)
},};
franchise.allMovies() // How to Train Your Dragon
我希望它评估为“未定义”的原因与本示例中的相同:
let franchise = {
name: 'How to Train Your Dragon',allMovies: function() {
function print() {
return this.name;
}
return print(); //undefined
}
};
解决方法
this.name
被评估(那个 Dragon 字符串)并作为参数传递给 console.log
。控制台与 this
无关 - 只是输出它的参数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。