gpt4 book ai didi

javascript - 从函数调用带有 'this' 的匿名函数

转载 作者:行者123 更新时间:2023-11-30 17:22:02 25 4
gpt4 key购买 nike

如何使用调用提供的函数上下文执行此匿名函数?

function test(text) {
this.first = 'test';
console.log(this.first);
}


(function(val){
return function(val) {
console.log(this.first);
}
}()).call(test)

最佳答案

我没有包含 var self = this;在进入返回函数之前:

(function(val){
var self = this; //add this line
return function(val) {
console.log(self.first);
}
}()).call(test)

关于javascript - 从函数调用带有 'this' 的匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com