VBScript:具有相同属性名称的类内的函数可见性
假设我们有一个脚本
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我几乎肯定没有办法在 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 toDate_
—that's not any better.您可以从类内部调用它,例如:
例如,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 useThis
in Javascript for example聚会已经太晚了,但是有一个使用
eval
函数的解决方案。有关详细信息,请参阅 eval 函数文档
Too late to the party, but there is a solution using
eval
function.see eval function docs for details