VBSctipt 5.0中的新特性
2024-07-21 02:15:27
供稿:網友
 
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、 正則表達式
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 = get|||