Javascript 调用module.exports上的函数时出现CommonJS非法调用错误

Javascript 调用module.exports上的函数时出现CommonJS非法调用错误,javascript,commonjs,webpack,method-invocation,Javascript,Commonjs,Webpack,Method Invocation,这样做很好: var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame; function _test() { console.log('hello from test'); } requestAnimationFrame(_test); 但是,将其移动到另

这样做很好:

var requestAnimationFrame = 
    window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame;

function _test() {
    console.log('hello from test');
}

requestAnimationFrame(_test);
但是,将其移动到另一个文件并使用CommonJS/webpack导出会导致:

Uncaught TypeError: Illegal invocation
(像这样:)

这可能是非常明显的,但在我看来,我不明白为什么这不起作用://

我在这里找到了答案:

似乎有些本机函数依赖于上下文,所以为了解决这个问题,我绑定到窗口:

module.exports.requestAnimationFrame = 
    (window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame).bind(window);
var poly = require('../utils/polyfills');
poly.requestAnimationFrame(_test);
module.exports.requestAnimationFrame = 
    (window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame).bind(window);