请解释这个requestAnimationFrame成语 [英] please explain this requestAnimationFrame idiom

查看:138
本文介绍了请解释这个requestAnimationFrame成语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多地方(例如如何使用requestAnimationFrame?)修复 window.requestAnimationFrame 如下。我不明白为什么分配的右侧包含在函数调用中。

there are a lot of places (e.g. How to use requestAnimationFrame?) that fix window.requestAnimationFrame as below. I do not understand why the right hand side of the assignment is wrapped in to a function call.


window.requestAnimFrame = (function(){
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback){
        window.setTimeout(callback, 1000 / 60);
    };
})();

推荐答案

我想知道同样的事情之前。我很确定谷歌真的很喜欢闭包。当然,那里没有一个变量,这是一个安全的事情。

I've wondered the same thing before. I'm pretty sure Google just really likes closures. Granted there is not one variable in there, it's a 'safe' thing to do.

据我所知,这是完全相同的事情,没有任何改变干扰名称空间(使用类似闭包的一般原因):

As far as I can tell, this is the exact same thing, without any change of interfering with namespaces (a general reason to use a closure like that):

window.requestAnimFrame = 
    window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback){
        window.setTimeout(callback, 1000 / 60);
    };

这篇关于请解释这个requestAnimationFrame成语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆