|
-
Feb 8th, 2020, 05:09 PM
#11
Re: Question 38: A strange problem with ActiveScript
 Originally Posted by dreammanor
Question 38: A strange problem with ActiveScript
If a function in ActiveScript has no input parameters, the function cannot return the function value, but it can be obtained using CallByName. E.g:
Code:
Set SC = New_c.ActiveScript("JScript9", False, False)
SC.AddCode "function Test() { " & _
" return arguments.length;" & _
"}"
Set CO = SC.CodeObject
MsgBox CO.Test()
This not a problem I can fix easily in RC5...
It is a general problem with the CodeObject of the MS ActiveScripting-support in JScript-mode...
Also the MS-ScriptControl shows this behaviour in JScript-mode, as the following test shows...:
Code:
Private Sub Form_Load()
With CreateObject("ScriptControl")
.Language = "JScript"
.AddCode "function Test(){ return 42 }"
MsgBox .CodeObject.Test() 'this returns the "whole Function" instead of 42
MsgBox .Run("Test") 'whilst this will return the correct answer
End With
With CreateObject("ScriptControl")
.Language = "VBScript"
.AddCode "Function Test(): Test = 42: End Function"
MsgBox .CodeObject.Test() 'this will return the correct answer
MsgBox .Run("Test") 'this will return the correct answer
End With
End Sub
As an explanation might serve, that JS allows to "pass functions around as normal Objects",
when you leave out the parentheses...
Here some DemoCode, which shows that the "weird behaviour" (as seen in the JS-CodeObject-call without arguments),
makes perfect sense in that "function-passing-context"...
Code:
Private Sub Form_Load()
Dim SC As cActiveScript, CO As Object, MyObj As Object
Set SC = New_c.ActiveScript("JScript9", False, False)
Set CO = SC.CodeObject
'let's say, you have a predefined Object in your JSCode like this one (MyObj)
SC.AddCode "var MyObj = {}; " & _
" MyObj.OnClick=null;" & _
" MyObj.fireOnClick = function(info){ if (this.OnClick) return this.OnClick(info)}"
'it contains an EventHandler-slot (OnClick) which is not (yet) defined
'but also (in the last line) a pre-implemented Method which "fires the Event to the Handler" (if there is one)
Set MyObj = CO.MyObj 'now, for convenience at the VB-COM-side, we store this JS-Obj in a VB-Object-Variable
'...
'later on in your code, you might want to define an Event-Handler for MyObj.OnClick
'so you will add a JS-function which implements such an EventHandler with your own specific code
SC.AddCode "function MyOnClickHandler(info){ return 'from inside my handler: ' + info }"
'what you can do now (and where the previously seen "weird behaviour" comes into play),
'is a "direct assignment of the function itself" (via the CodeObject)
Set MyObj.OnClick = CO.MyOnClickHandler '<- so here the "no passed arguments return the whole function"-case makes sense
MsgBox MyObj.fireOnClick("Hello World") 'test it
End Sub
I've marked the line which shows "why this stuff is, as it is" in dark-red above...
HTH
Olaf
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|