首页 > 开发 > 综合 > 正文

VBSctipt 5.0中的新特性

2020-02-03 16:41:03
字体:
来源:转载
供稿:网友
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

|||
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表