VBScript匿名关联数组? (比如在Javascript中)

时间:2010-11-15 21:15:06

标签: javascript arrays vbscript associative

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

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

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

谢谢,

2 个答案:

答案 0 :(得分:3)

Dictionary对象正是您正在寻找的。我已经成功地将它用于网站的多语言外观。这并不难。

请参阅:http://www.devguru.com/technologies/vbscript/13992.asp

答案 1 :(得分: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"_
)) %>

现在,无论doctype是什么,我都可以轻松控制图像标签的输出,而现在我可以通过正常索引的数组制作伪关联数组,从而更容易从SQL服务器输出图像。 / p>

解决这个问题的关键不在于制作图像标签,实际上是基于SQL服务器中的几个因素和数据构建数据和表单的整个视图,但我需要简化它来弄清楚它。现在它完美无缺。

感谢您的建议!