VBSctipt 5.0中的新特性

來源:互聯網
上載者:User
VBSctipt 5.0中的新特性

能夠在ASP中應用的特性包括了那些由指令碼引擎所提供的特性,這意味著VBScript的改進也可在ASP中應用。VBScript的改進如下所述:

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
…

3、 字串求值
Eval函數(過去只在JavaScript和Jscript中可用)目前在VBScript 5.0中已經得到了支援。允許建立包含指令碼代碼的字串,值可為True或False,並在執行後可得到一個結果:
…
datYourBirthday = Request.Form(“Birthday”)
strScript = “datYourBirthday = Date()”

If Eval(strScript) Then
Response.write “Happy Brithday!”
Else
Response.write “Have a nice day!”
End If
…

4、 語句執行
新的Execute函數允許執行一個字串中的指令碼代碼,執行方式與Eval函數相同,但是不返回結果。它可以用來動態建立代碼中稍後執行的過程,例如:
…
strCheckBirthday = “Sub CheckBirthday(datYourBirthday)” & vbCrlf_
& “ If Eval(datYourBirthday = Date()) Then” & vbCrlf_
& “ Response.Write “”Happy Birthday!””” & vbCrlf_
&” Else” & vbCrlf_
&” Response.write “”Have a nice day!””” & vbCrlf_
&” End If” & vbCrlf_
&”End Sub” & vbCrlf
Execute strCheckBirthday
CheckBirthday(Date())
…
一個斷行符號返回(如程式中示)或冒號字元“:”可用來分隔一個字串中的各條語句。

5、 設定地區
新的SetLocale方法可以用來改變指令碼引擎的當前地區,可正確顯示特殊的地區特定字元,如帶重音符的字元或來自不同字元集的字元。
StrCurrentLocale = GetLocale
SetLocale(“en-gb”)

6、 Regex
VBScript 5.0現在支援Regex(過去只在JavaScript、Jscript和其他語言中可用)。RegExp對象常用來建立和執行Regex,例如:
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 = Get

[1] [2] 下一頁  



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.