我最近阅读了一些代码,用于!!将变量转换为boolean以便在if语句中进行求值.这对我来说似乎有些多余,因为无论如何都要对变量进行布尔值的评估.这样做有什么性能优势,还是为了更好的浏览器支持?
示例代码:
var x = 0;
var atTop = x===window.scrollY;
if(!!atTop){
alert("At the top of the page.");
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我也看过这个不是布尔类型的操作数,但是我总是假设使用它if会评估变量的布尔值,因为Javascript中的所有值都是"truthy"或"falsey".
示例代码:
var x = 1;//or any other value including null, undefined, some string, etc
if(!!x){//x is "truthy"
//wouldn't using if(x) be the same???
console.log("something...");
}
Run Code Online (Sandbox Code Playgroud) 我同时调用许多 requestAnimationFrame。我如何取消所有 requestAnimationFrames 以便不再运行?