gpt4 book ai didi

javascript - try/catch 的替代方法,用于检查 javascript 中是否存在函数

转载 作者:行者123 更新时间:2023-11-30 07:45:15 24 4
gpt4 key购买 nike

使用以下语句是因为所讨论的方法 (refreshPartyList()) 可能并不总是被定义。

try {
parent.document.getElementById("myId").contentWindow.refreshPartyList(param1, param2);
}catch(e){}

目前正在使用上述方法并且工作正常,但是有没有更好的选择?

最佳答案

var e = parent.document.getElementById("myId").contentWindow;
if(e.refreshPartyList)
e.refreshPartyList(param1, param2);

关于javascript - try/catch 的替代方法,用于检查 javascript 中是否存在函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961494/

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