【发布时间】:2016-11-06 14:17:03
【问题描述】:
以下代码在 Firefox 浏览器的某些版本上出现错误 - linksHandle is not defined。
代码由一个函数组成,底部有一个名为 linksHandle 的函数。据我所知,这个函数应该在它定义的函数被调用时被提升。
因此,为事件“mMenuReady”定义的函数应该能够访问它,因为它包含了在其执行上下文中定义的所有函数和变量。
为什么某些 firefox 版本需要先定义函数声明 (linksHandle) 才能让 'mmenu' 回调包含函数?
document.addEventListener('readystatechange', function() {
if (document.readyState === 'interactive') {
if (typeof jQuery === 'function') {
// callback function that is invoked later by the event that is triggered -> $(window).trigger("mMenuReady")
$(window).on('mMenuReady', function() {
var links2 = Array.prototype.slice.call(document.querySelectorAll('#mm-mainMenu a'));
links2.forEach(linksHandle);
});
}
function linksHandle(elem) {
// function code
}
}
});
【问题讨论】:
标签: javascript function-declaration hoisting