简体   繁体   English

有人可以帮我理解下面的代码块吗?

[英]Could someone please help me to understand the following block of code?

I am a little confused by the following block of code. 我对下面的代码块感到有点困惑。 I will comment next to each line what I think it means. 我会在每行旁边评论一下我认为它意味着什么。 If someone could help me clarify any misunderstandings I have or confirm that I am in fact interpreting it correctly, I would very much appreciate that. 如果有人可以帮助我澄清我的任何误解或确认我实际上正确地解释了它,我会非常感激。

Here is this code in context: http://jsfiddle.net/MddHtt13/EMBZr/1/ 以下是上下文中的代码: http//jsfiddle.net/MddHtt13/EMBZr/1/

  if(!window.onload) { // If the window is not loaded then...
        window.onload = function() { //Assign an anonymous function to the onload event
            onLoad(); //Which, upon execution of the onload event execute the onLoad function
        };
    }
    else { //This is probably the most confusing part
        var oldWindowLoadFunction = window.onload; //Else if the window is loaded, assign the onload event to the variable oldWindowLoadFunction
        window.onload = function() {  //Then upon completion of the onload event, assign an anonymous function 
            oldWindowLoadFunction(); //which then re-executes the onload event 
            onLoad(); //and then executes the onLoad function 
        };
    }

The first thing I don't understand is the exclamation point next to window.onload 我不明白的第一件事是window.onload旁边的感叹号

if(!window.onload)

Why would I need to specify if the window is not yet loaded? 为什么我需要指定窗口是否尚未加载? Wouldn't I only want to attach the onLoad() function to the onload event so that upon completion it fires? 我不是只想将onLoad()函数附加到onload事件上,以便在完成时触发吗? Say with something like: 用以下的东西说:

window.onload = onLoad();

Why the extra steps? 为什么要额外的步骤? Why the if/else statement? 为什么if / else语句?


Secondly, why in the second half of the code block do I need to reload the page only to reattach the onLoad() function again? 其次,为什么在代码块的后半部分我需要重新加载页面才重新连接onLoad()函数? That sort of brings me back to what I just asked. 那种让我回到刚才问的问题。 Why does it have to be more complicated that simply writing: 为什么简单地写一下就更复杂了:

window.onload = onLoad();

Ofcourse when I change the code to be a simple statement, like the one above, it doesn't actually work. 当然,当我将代码更改为一个简单的语句时,如上所述,它实际上并不起作用。 However, I still don't completely understand the necessity of each part of the code block in question. 但是,我仍然不完全理解代码块的每个部分的必要性。
If someone could walk me through this in detail it would be extremely helpful. 如果有人能够详细介绍这一点,那将非常有帮助。

Edit: 编辑:
Thanks to the help of the folks below I replaced all of this code with one simple statement: 感谢下面的人的帮助,我用一个简单的声明替换了所有这些代码:

window.addEventListener('load', onLoad);

The ! ! is a boolean inversion: if not window.onload is null or undefined, or in plainer English, if the variable named onload in the object named window is not null or undefined. 是一个布尔反转:如果不是 window.onload为null或未定义,或者是更简洁的英语,如果名为window的对象中名为onload的变量不为null或未定义。

The logic basically says, if there is no onload function install mine. 逻辑基本上说,如果没有onload功能安装我的。 If there is an onload function install a new wrapper function which calls the existing function and then calls mine. 如果有onload函数,则安装一个新的包装函数,该函数调用现有函数然后调用我的函数。

None of the code "reloads" the page. 这些代码都没有“重新加载”页面。 You are confusing the assignment of the onload handler with loading the page. 您正在混淆onload处理程序的分配和加载页面。

What the function is doing is adding onload functionality to a window object which may already have onload functionality by chaining added function to the original, but only if that's necessary . 该函数正在做的是 onload功能添加到一个窗口对象,该窗口对象可能已经通过将添加的函数链接到原始函数而具有onload功能, 但仅在必要时

In today's world, it's all redundant, since you can just add an event listener to a list of functions to be executed for the onload event. 在今天的世界中,它都是多余的,因为您只需将事件监听器添加到要为onload事件执行的函数列表中。

if (!window.onload) 

This is checking to see if window.onload is not null or undefined. 这是检查window.onload是否为null或未定义。 If window.onload is already defined elsewhere then you might not want to replace it with your onLoad() function. 如果window.onload已在别处定义,那么您可能不希望将其替换为onLoad()函数。

Your block of code basically checks to see if window.onload is defined elsewhere. 您的代码块基本上会检查window.onload是否在其他地方定义。 If it isn't, assign onLoad() to window.onload . 如果不是,请将onLoad()分配给window.onload If it does, execute the existing window.onload and then call your onLoad() function as well. 如果是,则执行现有的window.onload ,然后调用onLoad()函数。

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

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