JavaScriptObject

Inherits: Reference < Object

A wrapper class for native JavaScript objects.

Description

JavaScriptObject is used to interact with JavaScript objects retrieved or created via JavaScript.get_interface, JavaScript.create_object, or JavaScript.create_callback.

Example:

  1. extends Node
  2. var _my_js_callback = JavaScript.create_callback(self, "myCallback") # This reference must be kept
  3. var console = JavaScript.get_interface("console")
  4. func _init():
  5. var buf = JavaScript.create_object("ArrayBuffer", 10) # new ArrayBuffer(10)
  6. print(buf) # prints [JavaScriptObject:OBJECT_ID]
  7. var uint8arr = JavaScript.create_object("Uint8Array", buf) # new Uint8Array(buf)
  8. uint8arr[1] = 255
  9. prints(uint8arr[1], uint8arr.byteLength) # prints 255 10
  10. console.log(uint8arr) # prints in browser console "Uint8Array(10) [ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 ]"
  11. # Equivalent of JavaScript: Array.from(uint8arr).forEach(myCallback)
  12. JavaScript.get_interface("Array").from(uint8arr).forEach(_my_js_callback)
  13. func myCallback(args):
  14. # Will be called with the parameters passed to the "forEach" callback
  15. # [0, 0, [JavaScriptObject:1173]]
  16. # [255, 1, [JavaScriptObject:1173]]
  17. # ...
  18. # [0, 9, [JavaScriptObject:1180]]
  19. print(args)

Note: Only available in the HTML5 platform.