Skip to content

Instantly share code, notes, and snippets.

@yu-tang
Created November 20, 2011 09:21
Show Gist options
  • Save yu-tang/1380055 to your computer and use it in GitHub Desktop.
Save yu-tang/1380055 to your computer and use it in GitHub Desktop.
引数を取らない JScript の関数オブジェクトを実行する方法が分からない
Option Explicit
' 引数を取らない JScript の関数オブジェクトを実行する方法が分からない
Sub Test()
Dim sc As New MSScriptControl.ScriptControl
Dim fx As Object
Dim s As String
s = "eval(function(){return x;});"
sc.Language = "JScript"
sc.AddCode "var x = 'Hello.';"
Set fx = sc.Eval(s) ' JScript の関数オブジェクトを取得する
Call DoSomething(fx)
End Sub
Sub DoSomething(CallbackFunction As Object)
Debug.Print CallbackFunction ' function(){return x;} <- 実行できない
Debug.Print CallbackFunction() ' function(){return x;} <- 実行できない
Debug.Print CallbackFunction(1) ' Hello. <- ダミーの引数を渡すと実行できるが…
End Sub
@honda0510
Copy link

ちょっと妥協が必要なのですが、
s = "(function(){return {'hoge':function(){return x;}};})();"
として、
Debug.Print CallByName(CallbackFunction, "hoge", VbMethod)
とするとできました。

@yu-tang
Copy link
Author

yu-tang commented Nov 20, 2011

ありがとうございます。
やっぱり素直な方法って無いんですかねー。
今のところ、ダミー引数渡すのが一番簡単な気がします…。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment