class_javascriptobject.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/4.0/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.0/doc/classes/JavaScriptObject.xml.
  6. .. _class_JavaScriptObject:
  7. JavaScriptObject
  8. ================
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A wrapper class for web native JavaScript objects.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. JavaScriptObject is used to interact with JavaScript objects retrieved or created via :ref:`JavaScriptBridge.get_interface<class_JavaScriptBridge_method_get_interface>`, :ref:`JavaScriptBridge.create_object<class_JavaScriptBridge_method_create_object>`, or :ref:`JavaScriptBridge.create_callback<class_JavaScriptBridge_method_create_callback>`.
  15. \ **Example:**\
  16. ::
  17. extends Node
  18. var _my_js_callback = JavaScriptBridge.create_callback(myCallback) # This reference must be kept
  19. var console = JavaScriptBridge.get_interface("console")
  20. func _init():
  21. var buf = JavaScriptBridge.create_object("ArrayBuffer", 10) # new ArrayBuffer(10)
  22. print(buf) # prints [JavaScriptObject:OBJECT_ID]
  23. var uint8arr = JavaScriptBridge.create_object("Uint8Array", buf) # new Uint8Array(buf)
  24. uint8arr[1] = 255
  25. prints(uint8arr[1], uint8arr.byteLength) # prints 255 10
  26. console.log(uint8arr) # prints in browser console "Uint8Array(10) [ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 ]"
  27. # Equivalent of JavaScriptBridge: Array.from(uint8arr).forEach(myCallback)
  28. JavaScriptBridge.get_interface("Array").from(uint8arr).forEach(_my_js_callback)
  29. func myCallback(args):
  30. # Will be called with the parameters passed to the "forEach" callback
  31. # [0, 0, [JavaScriptObject:1173]]
  32. # [255, 1, [JavaScriptObject:1173]]
  33. # ...
  34. # [0, 9, [JavaScriptObject:1180]]
  35. print(args)
  36. \ **Note:** Only available in the Web platform.
  37. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  38. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  39. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  40. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  41. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  42. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`