[英]Converting vbs to js: for each … in
我正在將一些舊的VBScript轉換為Javascript,其中兩行不知道如何正確轉換。 這是原始的VBS:
function getCount()
on error resume next
dim allitems, strItemID, icnt
icnt = 0
set allitems = dsoITEMS.selectNodes("//item")
for each node in allitems
strItemID = node.selectsinglenode("item_id").firstchild.nodevalue
if err then
exit for
end if
if strItemID <> "" then
icnt = icnt + 1
end if
next
set nodes = nothing
getCount = icnt
end function
這是我到目前為止的js:
function getCount(){
on error resume next;
var allitems, strItemID, icnt;
icnt = 0;
allitems = dsoITEMS.selectNodes("//item");
for each node in allitems;
strItemID = node.selectsinglenode("item_id").firstchild.nodevalue;
if(err){
exit for;
}
if(strItemID != ""){
icnt = icnt + 1;
}
next;
nodes = null;
getCount = icnt ;
}
我不知道如何轉換的行是“錯誤時恢復下一個”和“針對項目中的每個節點”
這是將VBS代碼轉換為JavaScript的方法:使用try {} catch {}捕獲錯誤。 遍歷項目集合時,可以使用如下所示的for循環進行迭代,並使用indexed屬性訪問一項。 當從函數返回值時,還需要使用“ return”關鍵字。
function getCount() {
var allitems, strItemID, icnt;
icnt = 0;
try {
allitems = dsoITEMS.selectNodes("//item");
for(var i = 0; i<= allitems.length; i++){
var node = allitems[i];
strItemID = node.selectsinglenode("item_id").firstchild.nodevalue;
if (strItemID !== ""){
icnt = icnt + 1;
}
}
}
catch (ex){
//Do something with the errors (if you want)
}
return icnt;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.