2021-01-26 17:06:29 +08:00
<?xml version="1.0" encoding="UTF-8" ?>
2022-02-14 21:18:53 +08:00
<class name= "JavaScriptObject" inherits= "RefCounted" version= "4.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2021-01-26 17:06:29 +08:00
<brief_description >
2022-09-02 20:40:41 +08:00
A wrapper class for web native JavaScript objects.
2021-01-26 17:06:29 +08:00
</brief_description>
<description >
2022-09-02 20:40:41 +08:00
JavaScriptObject is used to interact with JavaScript objects retrieved or created via [method JavaScriptBridge.get_interface], [method JavaScriptBridge.create_object], or [method JavaScriptBridge.create_callback].
2021-01-26 17:06:29 +08:00
Example:
[codeblock]
extends Node
2022-09-02 20:40:41 +08:00
var _my_js_callback = JavaScriptBridge.create_callback(self, "myCallback") # This reference must be kept
var console = JavaScriptBridge.get_interface("console")
2021-01-26 17:06:29 +08:00
func _init():
2022-09-02 20:40:41 +08:00
var buf = JavaScriptBridge.create_object("ArrayBuffer", 10) # new ArrayBuffer(10)
2021-01-26 17:06:29 +08:00
print(buf) # prints [JavaScriptObject:OBJECT_ID]
2022-09-02 20:40:41 +08:00
var uint8arr = JavaScriptBridge.create_object("Uint8Array", buf) # new Uint8Array(buf)
2021-01-26 17:06:29 +08:00
uint8arr[1] = 255
prints(uint8arr[1], uint8arr.byteLength) # prints 255 10
console.log(uint8arr) # prints in browser console "Uint8Array(10) [ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 ]"
2022-09-02 20:40:41 +08:00
# Equivalent of JavaScriptBridge: Array.from(uint8arr).forEach(myCallback)
JavaScriptBridge.get_interface("Array").from(uint8arr).forEach(_my_js_callback)
2021-01-26 17:06:29 +08:00
func myCallback(args):
# Will be called with the parameters passed to the "forEach" callback
# [0, 0, [JavaScriptObject:1173]]
# [255, 1, [JavaScriptObject:1173]]
# ...
# [0, 9, [JavaScriptObject:1180]]
print(args)
[/codeblock]
2022-08-29 02:27:45 +08:00
[b]Note:[/b] Only available in the Web platform.
2021-01-26 17:06:29 +08:00
</description>
<tutorials >
</tutorials>
</class>