0

精通 Javascript 和 PHP 的人知道如何使用 Object 构造函数来引用相当于匿名关联数组的内容,如下所示:

myFunction({
    "param1" : "value1",
    "param2" : "value2"
});

好处是不必命名目标函数的每个参数并且能够设置默认值。有谁知道如何在 VBScript 中类似地构建语句?我正在研究 Dictionary 课程,但在我看到一个例子之前,我认为我不会牢牢掌握如何在这里利用它来发挥我的优势。

谢谢,

4

2 回答 2

3

Dictionary 对象正是您要查找的对象。我已经成功地将它用于网站的多语言皮肤。使用起来并不难。

看:http://www.devguru.com/technologies/vbscript/13992.asp

于 2010-11-15T21:38:56.433 回答
1

你是对的,它并不难使用。这只是用普通阵列获得创意的问题。这是我所做的:

<%
    Function Img(aParamArray)
        Dim oImageTag,aImageTagKeys, val, param, key, output
        Set oImageTag = CreateObject("Scripting.Dictionary")

        For Each param In aParamArray
            val = Split(param, "::")
            If Ubound(val) = 1 Then
                oImageTag(val(0)) = val(1)
            End If
        Next

        aImageTagKeys = oImageTag.Keys
        Img = "<img "
        For Each key in aImageTagKeys
            If oImageTag(key) <> "" Then
                Img = Img & key & "=""" & oImageTag(key) & """ "
            End If
        Next

        If iDocType = 0 Or iDocType = 1 Or iDocType = 6 Then
            Img = Img & ">"
        Else
            Img = Img & "/>"
        End If
    End Function
%>

--或者我可以设置默认值并且只输出支持的属性--

<%
Function Img(aParamArray)
    Dim oImageTag,aImageTagKeys, val, param, key, output
    Set oImageTag = CreateObject("Scripting.Dictionary")
    oImageTag("src") = ""
    oImageTag("alt") = ""
    oImageTag("class") = ""
    oImageTag("id") = ""
    oImageTag("width") = ""
    oImageTag("height") = ""
    oImageTag("usemap") = ""
    oImageTag("title") = ""
    oImageTag("style") = ""
    oImageTag("dir") = ""
    oImageTag("lang") = ""
    oImageTag("ismap") = ""
    oImageTag("onabort") = ""
    oImageTag("onclick") = ""
    oImageTag("ondblclick") = ""
    oImageTag("onmousedown") = ""
    oImageTag("onmouseout") = ""
    oImageTag("onmouseover") = ""
    oImageTag("onmouseup") = ""
    oImageTag("onkeydown") = ""
    oImageTag("onkeypress") = ""
    oImageTag("onkeyup") = ""

    For Each param In aParamArray
        val = Split(param, "::")
        If Ubound(val) = 1 Then
            If oImageTag.Exists(val(0)) Then
                oImageTag(val(0)) = val(1)
            End If
        End If
    Next

    aImageTagKeys = oImageTag.Keys
    Img = "<img "
    For Each key in aImageTagKeys
        If oImageTag(key) <> "" Then
            Img = Img & key & "=""" & oImageTag(key) & """ "
        End If
    Next

    If iDocType = 0 Or iDocType = 1 Or iDocType = 6 Then
        Img = Img & ">"
    Else
        Img = Img & "/>"
    End If
End Function
%>

并这样称呼它:

<% =Img(Array(_
                "src::http://www.domain.com/img.jpg",_
                "alt::Some alt text",_
                "width::30",_
                "height::30",_
                "class::noborder"_
)) %>

现在,无论文档类型如何,我都可以轻松控制图像标签的输出,并且从 SQL 服务器输出图像会容易得多,因为我可以从正常索引的数组中创建一个伪关联数组。

解决这个问题的关键不是制作图像标签,它实际上是基于来自 SQL 服务器的多个因素和数据构建数据和表单的完整视图,但我需要对其进行简化才能弄清楚。现在它工作得很好。

谢谢你的建议!

于 2010-11-16T19:01:36.783 回答