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 exdivssion
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中的类似功能,函数可以被动态地指定为一个对象的属性。