2

例如,我如何在下面运行 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")

我喜欢用一行来显示给定字段的所有验证方式的想法。字符串的每个管道分隔部分都是方法的名称。

如果您有更好的设置建议,我会全力以赴!

4

6 回答 6

7

您可以使用以下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")

将“事物已被测试”发送给客户端。

所以这是您使用 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"))
于 2008-12-05T17:54:37.773 回答
2

如果您谈论的是 VBScript,它没有那种功能。(至少据我所知)我可能会这样尝试:

Select myvar
   case "test":
      test

   case "anotherSub":
      anotherSub

   else
      defaultSub

end select

我写 VBScript 已经有一段时间了(谢天谢地),所以我不确定我的语法有多好。

编辑-另一种策略

就个人而言,出于安全原因,我会执行上述操作。但是,如果您绝对不喜欢它,那么您可能想尝试在您的页面上使用不同的语言。我过去在我的经典 ASP 页面(服务器端)上同时使用了 Javascript 和 VBScript,并且能够从我当前的语言调用以另一种语言声明的函数。当我想用正则表达式做一些事情但在 VBScript 中时,这特别方便。

你可以尝试类似的东西

<script language="vbscript" runat="server">
    MyJavascriptEval myvar
</script>
<script language="javascript" runat="server">
    function MyJavascriptEval( myExpression)
    {
        eval(myExpression);
    }

    /* OR
    function MyJavascriptEval( myExpression)
    {
        var f = new Function(myExpression);
        f();
    }
    */
</script>

我没有在经典的 ASP 页面中对此进行测试,但我认为它已经足够接近,只需稍加调整即可使用。

于 2008-12-02T22:21:27.553 回答
2

在 ASP/VBScript 中使用“执行”语句。

Execute "Response.Write ""hello world"""
于 2008-12-06T13:30:58.337 回答
1

PHP 动态调用或创建函数的能力是导致不良编程实践的技巧。您需要解释您要完成的工作(而不是如何完成)并学习正确的编码方式。

仅仅因为你可以做某事,并不能使它正确或一个好主意。

于 2008-12-02T22:27:05.357 回答
0

ASP 不支持这种方式的后期绑定。从更大的意义上说,你想做什么?解释一下,有人可以告诉你如何在asp中完成它。

于 2008-12-02T22:20:43.530 回答
0

此外,您可能会考虑“客观化”验证功能。在 VB 脚本中创建类是可能的(尽管没有广泛使用)。

<%
Class User
' declare private class variable
Private m_userName

' declare the property
Public Property Get UserName
  UserName = m_userName
End Property
Public Property Let UserName (strUserName)
  m_userName = strUserName
End Property

' declare and define the method
Sub DisplayUserName
  Response.Write UserName
End Sub

End Class
%> 
于 2008-12-05T18:14:25.440 回答