Asp-Classic

從經典 ASP 中的函式提前返回

  • February 4, 2010

有沒有辦法從經典 ASP 中的函式提前返回而不是執行函式的全部長度?例如,假設我有這個功能……

Function MyFunc(str)
 if (str = "ReturnNow!") then
   Response.Write("What up!")       
 else
   Response.Write("Made it to the end")     
 end if
End Function

我可以這樣寫嗎…

Function MyFunc(str)
 if (str = "ReturnNow!") then
   Response.Write("What up!")       
   return
 end if

 Response.Write("Made it to the end")     
End Function

請注意我在經典 ASP 中當然不能執行的 return 語句。有沒有辦法在返回語句所在的位置中斷程式碼執行?

是的,使用exit function.

Function MyFunc(str)
 if str = "ReturnNow!" then
   Response.Write("What up!")       
   Exit Function
 end if

 Response.Write("Made it to the end")     
End Function

我通常在從函式返回值時使用它。

Function usefulFunc(str)
  ''# Validate Input
  If str = "" Then
     usefulFunc = ""
     Exit Function
  End If 

  ''# Real function 
  ''# ...
End Function

引用自:https://stackoverflow.com/questions/2201072