简体   繁体   English

这个for循环应该做什么?

[英]What is this for-loop supposed to do?

I've been trudging through some Javascript code and encountered this loop 我一直在浏览一些Javascript代码,并遇到了这个循环

for (var w = window; w.window === window.window.window; w = w.window) {
    w.w = w.prompt("Enter password");
    if (w.w === "swordfish") break;
    w.alert("Incorrect password.");
}
w.alert("Welcome, authenticated user!");

This code doesn't really make any sense to me. 这段代码对我来说真的没有任何意义。 What in the world is going on here and how does it work? 世界上正在发生什么,它如何运作?

window is always equal to window.window....window , so the loop will never end, unless the password is correct. window始终等于window.window....window ,因此,除非密码正确,否则循环永远不会结束。

A for(;;) loop has the following signature: for(;;)循环具有以下签名:

for (init; test; increment);

It keeps continuing until test is false. 它一直持续到test为假。 Since window === window is always true, the loop keeps running, until break is encountered. 由于window === window始终为true,因此循环一直运行,直到遇到break For clarification, w always refers to window . 为了澄清起见, w始终指window

Well the alertbox keeps popping up for an indefinite period of time unless you provide the password " swordfish " ! 除非您提供密码“ swordfish ”,否则警报框会无限期弹出!

If you encounter such script , Just disable JavaScript and view the Source , grab the password (make a note of it) , enable the JS , run the script again and enter the password to see what awaits ;) 如果遇到这样的脚本,只需禁用JavaScript并查看Source,获取密码(记下密码),启用JS,再次运行脚本,然后输入密码以查看等待的内容;)

您需要查看上一行才能完全理解此代码:

var window = (function () { return this; })();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM