PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/AutoHotkey.docset/Contents/Resources/Documents/objects/Functor.htm

https://gitlab.com/ahkscript/Autohotkey.docset
HTML | 78 lines | 70 code | 7 blank | 1 comment | 0 complexity | 4fcf65cd45a4e635ba0b04c9c8ae645f MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Function Objects</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <link href="../static/theme.css" rel="stylesheet" type="text/css" />
  8. <script src="../static/content.js" type="text/javascript"></script>
  9. </head>
  10. <body>
  11. <h1>Function Objects</h1>
  12. <p>"Function object" usually means any of the following:</p>
  13. <ul>
  14. <li>A reference to a <a href="Func.htm">Func object</a>, which represents an actual <a href="../Functions.htm">function</a>; either built-in or defined by the script.</li>
  15. <li>A user-defined object which can be called like a function. This is sometimes also referred to as a "functor".</li>
  16. <li>Any other object which can be called like a function, such as a <a href="#BoundFunc">BoundFunc object</a> or a JavaScript function object returned by a COM method.</li>
  17. </ul>
  18. <p>Function objects can be used with the following:</p>
  19. <ul>
  20. <li><a href="../commands/Gui.htm#label">Gui control events</a> (g-labels)</li>
  21. <li><a href="../commands/Hotkey.htm#Functor">Hotkey</a></li>
  22. <li><a href="../commands/Menu.htm#Functor">Menu</a></li>
  23. <li><a href="../commands/OnClipboardChange.htm">OnClipboardChange()</a></li>
  24. <li><a href="../commands/OnExit.htm">OnExit()</a></li>
  25. <li><a href="../commands/OnMessage.htm">OnMessage()</a></li>
  26. <li><a href="../commands/SetTimer.htm#Functor">SetTimer</a></li>
  27. </ul>
  28. <h2 id="User-Defined">User-Defined</h2>
  29. <!-- TODO: Explain how a user-defined object could be useful -->
  30. <p>User-defined function objects should follow this general pattern:</p>
  31. <pre>class YourClassName {
  32. Call(a, b) { <em>; Declare parameters as needed, or an <a href="../Functions.htm#Variadic">array*</a>.</em>
  33. <em>;...</em>
  34. }
  35. __Call(method, args*) {
  36. if (method = "") <em>; For <a href="../Functions.htm#DynCall">%fn%()</a> or fn.()</em>
  37. return this.Call(args*)
  38. if (IsObject(method)) <em>; If this function object is being used as a method.</em>
  39. return this.Call(method, args*)
  40. }
  41. <em>;...</em>
  42. }
  43. </pre>
  44. <p>Exactly which parts are needed depends on the usage:</p>
  45. <ul>
  46. <li><code>method</code> is an empty string if the script used <code><a href="../Functions.htm#DynCall">%this%()</a></code> or <code>this.()</code>.</li>
  47. <li>If the object is being used as a method, <code>IsObject(method)</code> is true and <code>method</code> contains a reference to the target object. For example, if <code>x.y</code> refers to <code>this</code> function object, <code>x.y()</code> &rarr; <code>this[x]()</code> &rarr; <code>this.__Call(x)</code> &rarr; <code>this.Call(x)</code>.</li>
  48. <li><span class="ver">[v1.1.20+]:</span> If the object is being used by one of the built-in functions which accept a callback function, such as OnMessage or SetTimer, only the Call method is needed.</li>
  49. </ul>
  50. <p>The work can also be done directly in __Call. However, having __Call redirect to Call is recommended to ease the transition to AutoHotkey v2, which will change the behaviour of <code>%this%()</code> and method calls to call the Call method directly.</p>
  51. <h2 id="BoundFunc">BoundFunc Object <span class="ver">[v1.1.20+]</span></h2>
  52. <p>Acts like a function, but just passes predefined parameters to another function.</p>
  53. <p>There are two ways that BoundFunc objects can be created:</p>
  54. <ul>
  55. <li>By calling the <a href="Func.htm#Bind">Func.Bind()</a> method, which binds parameter values to a function.</li>
  56. <li>By calling the <a href="../commands/ObjBindMethod.htm">ObjBindMethod()</a> function, which binds parameter values and a method name to a target object.</li>
  57. </ul>
  58. <p>BoundFunc objects can be called as shown in the example below. No other methods are supported. When the BoundFunc is called, it calls the function or method to which it is bound, passing any bound parameters followed by any which were passed by the caller. For example:</p>
  59. <pre>fn := Func("RealFn").Bind(1)
  60. %fn%(2) <em>; Shows "1, 2"</em>
  61. fn.Call(3) <em>; Shows "1, 3"</em>
  62. RealFn(a, b) {
  63. MsgBox %a%, %b%
  64. }</pre>
  65. <p><a href="../commands/ObjBindMethod.htm">ObjBindMethod()</a> can be used to bind to a method when it isn't possible to retrieve a reference to the method itself. For example:</p>
  66. <pre>file := FileOpen(A_ScriptFullPath, "r")
  67. getLine := ObjBindMethod(file, "ReadLine")
  68. MsgBox % %getLine%() <em>; Shows the first line of this file.</em></pre>
  69. <p>For a more complex example, see <a href="../commands/SetTimer.htm#ExampleClass">SetTimer</a>.</p>
  70. </body>
  71. </html>