gpt4 book ai didi

Javascript "this"范围

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:31 27 4
gpt4 key购买 nike

我正在编写一些 JavaScript 代码。我对这个关键字有点困惑。如何在 dataReceivedHandler 函数中访问 logger 变量?

MyClass: {
logger: null,
init: function() {
logger = LogFactory.getLogger();
},
loadData: function() {
var dataReceivedHandler = function() {
// how to access the logger variable here?
}

// more stuff
}
};

最佳答案

您可以在 loadData 函数中执行类似的操作来访问您的对象...

MyClass: {
logger: null,
init: function() {
this.logger = LogFactory.getLogger();
},
loadData: function() {
var self = this;
var dataReceivedHandler = function() {
// how to access the logger variable here?
self.logger.log('something');
}

// more stuff
}
};

关于Javascript "this"范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15823948/

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