gpt4 book ai didi

javascript - 调用赋值给变量的 JS 函数

转载 作者:行者123 更新时间:2023-11-30 08:16:12 27 4
gpt4 key购买 nike

前段时间我问了一个关于同一个问题的问题,@BenAlabaster 解释得很透彻in his answer ,我想我在这里仍然缺少一段逻辑。

我尝试在最后一个函数中调用分配给变量的函数,如下所示。好吧,它没有用。假设它发生是因为变量 functionThreeaddOnload() 函数之外,我试图将它移到里面,浏览器就崩溃了。

addOnload(functionOne);
addOnload(functionTwo);
addOnload(functionThree);

function addOnload(newFunction){
oldOnload = window.onload;

if(typeof oldOnload == "function"){
window.onload = function(){
if(oldOnload){
oldOnload();
}
newFunction(); //This is what I missed, and that's what caused the crash.
}
}
else{
window.onload = newFunction;
}
}

function functionOne(){
alert("This is the first function on this page!");
}

function functionTwo(){
alert("This is the second function on this page!");
}

functionThree = function(){
alert("This is the third function on this page!");
}

最佳答案

您的 addOnloadFunction 有一个小问题:它应该是:

function addOnload(newFunction){
var oldOnload = window.onload;

if(typeof oldOnload == "function"){
window.onload = function(){
newFunction();
oldOnload();
}
}
else{
window.onload = newFunction;
}
}

关于javascript - 调用赋值给变量的 JS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3596634/

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