When I use requestAnimationFrame to do some native supported animation with below code:
var support = {
animationFrame: window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
};
support.animationFrame(function() {}); //error
support.animationFrame.call(window, function() {}); //right
Directly calling the support.animationFrame will give…
Uncaught TypeError: Illegal invocation
in Chrome. Why?
In your code you are assigning a native method to a property of custom object.
When you call
support.animationFrame(function () {}), it is executed in the context of current object (ie support). For the native requestAnimationFrame function to work properly, it must be executed in the context ofwindow.So the correct usage here is
support.animationFrame.call(window, function() {});.The same happens with alert too:
Another option is to use Function.prototype.bind() which is part of ES5 standard and available in all modern browsers.