1、 在腳本中使用類 在VBScript中實現完整的VB類(class)模型,但明顯的例外是在ASP服務器端的腳本事件。可以在腳本中創建類,使它們的屬性和方法能夠和用于頁面的其余代碼,例如: Class MyClass
PRivate m_HalfValue ‘Local variable to hold value of HalfValue
Public Property Let HalfValue(vData) ‘executed to set the HalfValue property If vData > 0 Then m_HalfValue = vData End Property
Public Property Get HalfValue() ‘executed to return the HalfValue property HalfValue = m_HalfValue End Property
Public Function GetResult() ‘implements the GetResult method GetResult = m_HalfVaue * 2 End Function End Class
Set ObjThis = New MyClass
ObjThis.HalfValue = 21
Response.Write “Value of HalfValue property is “ & objThis.HalfValue & “<BR>” Response.Write “Result of GetResult method is “ & objThis.GetResult & “<BR>” … 這段代碼產生如下結果: Value of HalfValue property is 21 Result of GetResult method is 42
2、 With結構 VBScript 5.0支持With結構,使訪問一個對象的幾個屬性或方法的代碼更加緊湊: … Set objThis = Server.CreateObject(“This.object”)
With objThis .Property1 = “This value” .Property2 = “Another value” TheResult = .SomeMethod End With …
6、 正則表達式 VBScript 5.0現在支持正則表達式(過去只在Javascript、Jscript和其他語言中可用)。RegExp對象常用來創建和執行正則表達式,例如: StrTarget = “test testing tested attest late start” Set objRegExp = New RegExp ‘create a regular expression
ObjRegExp.Pattern = “test*” ‘set the search pattern ObjRegExp.IgnoreCase = False ‘set the case sensitivity ObjRegExp.Global = True ‘set the scope
Set colMatches = objRegExp.Execute(strTarget) ‘execute the search
For Each Match in colMatches ‘iterate the colMatches collection Response.Write “Match found at position” & Match.FirstIndex & “.” Resposne.Write “Matched value is ‘” & Match.Value & “’.<BR>” Next 執行結果如下: Match found at position 0. Matched value is ‘test’. Match found at position 5. Matched value is ‘test’. Match found at position 13. Matched value is ‘test’; Match found at position 22. Matched value is ‘test’.
7、 在客戶端VBScript中設置事件處理程序 這不是直接應用于ASP的腳本技術,這個新的特性在編寫客戶端的VBScript時是很有用的。現在可以動態指定一個函數或子程序與一個事件相關聯。例如,假設一個函數的名稱為MyFunction(),可把這指定給按鈕的OnClick事件: Function MyFunction() … Function implementation code here … End Function … Set objCimButton = document.all(“cmdButton”) Set objCmdButton.OnClick = GetRef(“Myfunction”) 這提供了JavaScript和Jscript中的類似功能,函數可以被動態地指定為一個對象的屬性。