例如,我如何在下面运行me.test?
myvar = 'test' me.myvar
ASP查找方法"myvar"但找不到它.在PHP中,我可以简单地说$ me - > $ myvar,但ASP的语法不区分变量和方法.建议?
与此密切相关,ASP Classic中是否有method_exists函数?
提前致谢!
编辑:我正在写一个验证类,并希望通过管道分隔字符串调用方法列表.
例如,要验证名称字段,我会调用:
validate("required|min_length(3)|max_length(100)|alphanumeric")
我喜欢有一条线来显示给定字段的所有验证方式.并且字符串的每个管道分隔部分是方法的名称.
如果您有更好的设置建议,我会全力以赴!
您可以使用以下GetRef
函数在VBScript中实现此目的: -
Function Test(val) Test = val & " has been tested" End Function Dim myvar : myvar = "Test" Dim x : Set x = GetRef(myvar) Response.Write x("Thing")
将"Thing已经过测试"发送给客户.
所以这是使用GetRef的验证要求: -
validate("Hello World", "min_length(3)|max_length(10)|alphanumeric") Function required(val) required = val <> Empty End Function Function min_length(val, params) min_length = Len(val) >= CInt(params(0)) End Function Function max_length(val, params) max_length = Len(val) <= CInt(params(0)) End Function Function alphanumeric(val) Dim rgx : Set rgx = New RegExp rgx.Pattern = "^[A-Za-z0-9]+$" alphanumeric = rgx.Test(val) End Function Function validate(val, criterion) Dim arrCriterion : arrCriterion = Split(criterion, "|") Dim criteria validate = True For Each criteria in arrCriterion Dim paramListPos : paramListPos = InStr(criteria, "(") If paramListPos = 0 Then validate = GetRef(criteria)(val) Else Dim paramList paramList = Split(Mid(criteria, paramListPos + 1, Len(criteria) - paramListPos - 1), ",") criteria = Left(criteria, paramListPos - 1) validate = GetRef(criteria)(val, paramList) End If If Not validate Then Exit For Next End Function
提供这个之后我不得不说,如果你熟悉PHP,那么JScript将是服务器上更好的选择.在Javascript中,您可以调用这样的方法: -
function test(val) { return val + " has been tested"; ) var myvar = "test" Response.Write(this[myvar]("Thing"))