class_javascriptobject.rst 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the JavaScriptObject.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_JavaScriptObject:
  6. JavaScriptObject
  7. ================
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. A wrapper class for native JavaScript objects.
  10. Description
  11. -----------
  12. JavaScriptObject is used to interact with JavaScript objects retrieved or created via :ref:`JavaScript.get_interface<class_JavaScript_method_get_interface>`, :ref:`JavaScript.create_object<class_JavaScript_method_create_object>`, or :ref:`JavaScript.create_callback<class_JavaScript_method_create_callback>`.
  13. Example:
  14. ::
  15. extends Node
  16. var _my_js_callback = JavaScript.create_callback(self, "myCallback") # This reference must be kept
  17. var console = JavaScript.get_interface("console")
  18. func _init():
  19. var buf = JavaScript.create_object("ArrayBuffer", 10) # new ArrayBuffer(10)
  20. print(buf) # prints [JavaScriptObject:OBJECT_ID]
  21. var uint8arr = JavaScript.create_object("Uint8Array", buf) # new Uint8Array(buf)
  22. uint8arr[1] = 255
  23. prints(uint8arr[1], uint8arr.byteLength) # prints 255 10
  24. console.log(uint8arr) # prints in browser console "Uint8Array(10) [ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 ]"
  25. # Equivalent of JavaScript: Array.from(uint8arr).forEach(myCallback)
  26. JavaScript.get_interface("Array").from(uint8arr).forEach(_my_js_callback)
  27. func myCallback(args):
  28. # Will be called with the parameters passed to the "forEach" callback
  29. # [0, 0, [JavaScriptObject:1173]]
  30. # [255, 1, [JavaScriptObject:1173]]
  31. # ...
  32. # [0, 9, [JavaScriptObject:1180]]
  33. print(args)
  34. **Note:** Only available in the HTML5 platform.
  35. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  36. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  37. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`