VBScript:具有相同属性名称的类内的函数可见性

发布于 2024-10-20 04:26:29 字数 692 浏览 1 评论 0原文

假设我们有一个脚本

Option Explicit

Class CClass
    Private m_date

    Private Sub Class_Initialize()
        m_date = CDate("1970-01-01 00:00:00")
    End Sub

    Public Function Foo()
        Dim d : d = Date()
        WScript.Echo "d is " & FormatDateTime(d, vbGeneralDate)
    End Function

    Public Property Get Date()
        Date = m_date
    End Property

    Public Property Let Date(p_date)
        m_date = CDate(p_date)
    End Property

End Class

Dim obj : Set obj = NEW CClass
Call obj.Foo()

,类函数 CClass.Foo() 如何调用内置 VBScript 函数 Date() 而不受属性 CClass.Date 的干扰?

我当前的解决方案是引入一个可以调用的虚拟 Date_() 函数。但这似乎是错误的。我认为应该有某种方法来指定我们想要调用类范围之外的东西。

Given that we have a script

Option Explicit

Class CClass
    Private m_date

    Private Sub Class_Initialize()
        m_date = CDate("1970-01-01 00:00:00")
    End Sub

    Public Function Foo()
        Dim d : d = Date()
        WScript.Echo "d is " & FormatDateTime(d, vbGeneralDate)
    End Function

    Public Property Get Date()
        Date = m_date
    End Property

    Public Property Let Date(p_date)
        m_date = CDate(p_date)
    End Property

End Class

Dim obj : Set obj = NEW CClass
Call obj.Foo()

How can class function CClass.Foo() call built-in VBScript function Date() without the property CClass.Date interfering?

My current solution is to introduce a dummy Date_() function which can be called instead. But that just seems wrong. I'm thinking there should be some way to specify that we want to call something outside the class scope.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的快感 2024-10-27 04:26:29

我几乎肯定没有办法在 VBScript 中完成您所要求的操作。

但即使您能够找到一种方法来做到这一点,您也确实不应该。您需要为自己的函数选择不与内置函数的名称冲突的名称。对于像 VBScript 这样的动态脚本语言来说,其他任何事情都是完全无法维护的。

为您的Date 属性选择一个不同的名称。最好是更具描述性的东西:该属性返回什么样的日期?日期指的是什么?它可能会被如何使用?无论您做什么,都不要将其重命名为 Date_——那样也好不了多少。

I am almost positive that there is no way to do what you're asking in VBScript.

But even if you could figure out a way to do this, you really shouldn't. You need to choose names for your own functions that don't conflict with the names of built-in functions. Anything else is completely unmaintainable for a dynamic scripting language like VBScript.

Pick a different name for your Date property. Preferably something more descriptive: what kind of date does that property return? What does the date refer to? How is it likely to be used? Whatever you do, don't rename it to Date_—that's not any better.

只涨不跌 2024-10-27 04:26:29

您可以从类内部调用它,例如:
例如,VBScript 中的Dim d : d = me.Date()

Me 与 Javascript 中的 This 相同

You can call it from inside the class like:
Dim d : d = me.Date()

Me in VBScript is the same as you use This in Javascript for example

凉城凉梦凉人心 2024-10-27 04:26:29

聚会已经太晚了,但是有一个使用 eval 函数的解决方案。
有关详细信息,请参阅 eval 函数文档

Class CClass
    Private m_date

    Private Sub Class_Initialize()
        m_date = CDate("1970-01-01 00:00:00")
    End Sub

    Public Function Foo()
        Dim d : d = eval("Date()")
        WScript.Echo "d is " & FormatDateTime(d, vbGeneralDate)
    End Function

    Public Property Get Date()
        Date = m_date
    End Property

    Public Property Let Date(p_date)
        m_date = CDate(p_date)
    End Property

End Class

Dim obj : Set obj = NEW CClass
Call obj.Foo()

Too late to the party, but there is a solution using eval function.
see eval function docs for details

Class CClass
    Private m_date

    Private Sub Class_Initialize()
        m_date = CDate("1970-01-01 00:00:00")
    End Sub

    Public Function Foo()
        Dim d : d = eval("Date()")
        WScript.Echo "d is " & FormatDateTime(d, vbGeneralDate)
    End Function

    Public Property Get Date()
        Date = m_date
    End Property

    Public Property Let Date(p_date)
        m_date = CDate(p_date)
    End Property

End Class

Dim obj : Set obj = NEW CClass
Call obj.Foo()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文