gpt4 book ai didi

javascript - 检查 vendor Ex 的正确方法。 webkitRequestAnimationFrame || mozRequestAnimationFrame

转载 作者:行者123 更新时间:2023-11-30 17:11:29 25 4
gpt4 key购买 nike

我从 Google ( https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery ) 获得了这段代码:

var raf = requestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || msRequestAnimationFrame;

这如何有效?如果未定义 requestAnimationFrame,javascript 将在检查 moz、webkit 或 ms 之前崩溃。

不应该是:

var raf =   
typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame :
typeof mozRequestAnimationFrame !== 'undefined' ? requestAnimationFrame :
typeof webkitRequestAnimationFrame !== 'undefined' ? requestAnimationFrame :
typeof msRequestAnimationFrame!== 'undefined' ? requestAnimationFrame : null;

最佳答案

我见过的方法是在每个变量前加上前缀window.。如果您尝试访问对象上未定义的变量,而不是未定义的属性,JavaScript 将抛出错误。

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

关于javascript - 检查 vendor Ex 的正确方法。 webkitRequestAnimationFrame || mozRequestAnimationFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com