gpt4 book ai didi

javascript - JS/jQuery - 关于创建可在多个函数中使用的变量的建议

转载 作者:行者123 更新时间:2023-11-28 16:34:39 26 4
gpt4 key购买 nike

我想创建一系列如下变量,每个变量都有设置:

var panel_myfeed = new Array();
panel_myfeed[0]="/myfeed";
panel_myfeed[1]="#view-panel-myfeed";
panel_myfeed[2]="XXXXXXXX";

var panel_OtherPanel = new Array();
panel_OtherPanel[0]="/otherurl";
panel_OtherPanel[1]="#other-url-panel";

var panel_OtherPanel2 = new Array();
panel_OtherPanel2[0]="/otherurl2";
panel_OtherPanel2[1]=".other-url-panel2";

然后我想要两个单独的函数可以使用这些变量

function WhichPanel(url) {
*** Given a URL var like /myfeed, which which panel[0] this belongs to so I can get the other variables
** do something now that I have the settings above
}

function KillPanel(url) {
*** Given a URL var like /myfeed, which which panel[0] this belongs to so I can get the other variables
** do something now that I have the settings above
}

建议?谢谢

最佳答案

对象映射在这里是最简单的,如下所示:

var panels = { "/myfeed": "#view-panel-myfeed", 
"/otherurl": "#other-url-panel",
"/otherurl2": ".other-url-panel2" }

然后在您的函数中,您只需执行 panels[url] 即可获取与其配套的选择器。如果您需要其他属性,只需使用对象代替值,如下所示:

var panels = { "/myfeed": { selector: "#view-panel-myfeed", other: "value" }, 
"/otherurl": { selector: "#other-url-panel" },
"/otherurl2": { selector: ".other-url-panel2" } }

然后要获取选择器,它是 panels[url].selector,其他属性的 .property 等。

关于javascript - JS/jQuery - 关于创建可在多个函数中使用的变量的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4514611/

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