我正在查看following code from Paul Irish on the requestAnimationFrame shim layer。我想从另一种语言中生成这个js,所以我试图把我的头围绕到正在发生的事情上。 (你可以see it being used in an animation here)。解释请求动画帧垫片层
window.requestAnimFrame = (function(){ //1
return window.requestAnimationFrame || //2
window.webkitRequestAnimationFrame || //3
window.mozRequestAnimationFrame || //4
function(callback){ //5
window.setTimeout(callback, 1000/60); //6
}; //7
})(); //8
//9
(function animloop(){ //10
requestAnimFrame(animloop); //11
render(); //12
})(); //13
我的问题是 - 第2-4行发生了什么?我们似乎正在获取函数句柄,并将它们连接到OR操作中 - 然后将OR
操作的结果分配给第1行中的匿名函数 - 然后将其分配给var window.requestAnimFrame
。
编辑: 感谢所有有用的答案 - 我只是想确保我有这个正确的 - 是下面发生了什么事的精确表示?(Here is the original)(Here is the version with the more explicit code。)
var myfunc = function() {
if (typeof(window.requestAnimationFrame) != "undefined") {
return window.requestAnimationFrame;
} else if (typeof(window.webkitRequestAnimationFrame) != "undefined") {
return window.webkitRequestAnimationFrame;
} else if (typeof(window.mozRequestAnimationFrame) != "undefined") {
return window.mozRequestAnimationFrame;
} else {
return function(callback) {
window.setTimeout(callback, 1000/60);
};
}
};
window.requestAnimFrame = myfunc;
是的,这是一个合理的解释。然而在JavaScript中,你通常使用伪造而不是显式检查未定义或null。 – Woody