关闭和功能提升-不适用于 firefox
Closure and function hoisting- not working on firefox
以下代码在 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
}
}
});
are only allowed since ES6. They do hoist inside your if
body (not to the whole function), but not in older versions of FF that did implement them as "function statements" that were not hoisted (and actually ) having caused issues like yours.
以下代码在 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
}
}
});
if
body (not to the whole function), but not in older versions of FF that did implement them as "function statements" that were not hoisted (and actually