You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JavascriptInterop.md
+19-39Lines changed: 19 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,55 +13,35 @@ JS.debugger()
13
13
14
14
# Getting the type of a value
15
15
JS.typeof(my_value)
16
-
17
-
# Creating a new JavaScript Map
18
-
map =JS.new(JS.Map, [])
19
16
```
20
17
21
-
### Accessing Global Objects, Functions, and Properties
22
-
23
-
In order to interact with JavaScript things in the global scope, append "JS" to them. The global scope corresponds to whatever the global object is in the JavaScript environment you are in. For example, in a browser this would be `window` or `self`:
18
+
### Foreign Function Interface
24
19
25
-
```elixir
26
-
# Calling alert
27
-
JS.alert("hello")
20
+
ElixirScript calls JavaScript modules through a Foreign Function Interface (FFI). A foreign module is defined by creating a new module and adding `use ElixirScript.FFI` to it.
28
21
29
-
# Calling a method on Object
30
-
JS.Object.keys(my_object)
22
+
Here is an example of a foreign module for a JSON module
31
23
32
-
# Creating a new JavaScript Date
33
-
JS.new(JS.Date, [])
24
+
```elixir
25
+
defmoduleMyApp.JSONdo
26
+
useElixirScript.FFI
34
27
35
-
# Getting the outer width
36
-
JS.outerWidth
28
+
foreign stringify(map)
29
+
foreign parse(string)
30
+
end
37
31
```
38
32
39
-
### JavaScript modules
40
-
41
-
ElixirScript can use JavaScript modules from the supported modules systems.
42
-
In order to do so, you must tell ElixirScript about them upfront.
33
+
Foreign modules map to JavaScript files that export functions defined with the `foreign` macro.
34
+
ElixirScript expects JavaScript modules to be in the `priv/elixir_script` directory.
35
+
These modules are copied to the output directory upon compilation.
43
36
44
-
If using ElixirScript in a mix project, you can do so inside of the ElixirScript configuration keyword list
37
+
For our example, a JavaScript file must be placed at `priv/elixir_script/my_app/json.js`.
45
38
46
-
```elixir
47
-
defprojectdo
48
-
[
49
-
app::my_project,
50
-
elixir_script: [
51
-
format::es,
52
-
js_modules: [
53
-
{React, "react"},
54
-
{ReactDOM, "react-dom"}
55
-
]
56
-
]
57
-
]
58
-
end
59
-
```
60
-
61
-
Interacting with these modules works the same as interacting with an ElixirScript module
0 commit comments