elixir_script v0.20.0 ElixirScript.JS

This module defines macros and functions which implement JavaScript functionality that may not translate easily to Elixir. For instance, creating a new object, or updating an existing one.

Summary

Functions

Returns a reference to the global JavaScript object

Macros

Imports a JavaScript module

Imports a JavaScript module

Determines if value is an instance of type

Creates new JavaScript objects

Throws the term given

Returns the type of the given value

Updates an existing JavaScript object

Functions

global()

Returns a reference to the global JavaScript object.

In browsers this would be window or self. In node this would be the global object.

Macros

import(module)

Imports a JavaScript module.

Works like import/2, but tries to infer the path to the module. Only works for default imports. Uses Macro.underscore to infer path.

ex: JS.import React #translates to “import React from ‘react’”

import(module, from)

Imports a JavaScript module.

Elixir modules can use the normal import, alias and require, but JavaScript modules work differently and have to be imported using this.

If module is not a list, then it is treated as a default import, otherwise it is not.

ex: JS.import A, “a” #translates to “import A from ‘a’”

JS.import [A, B, C], “a” #translates to “import {A, B, C} from ‘a’”

instanceof(value, type)

Determines if value is an instance of type.

new(module, params)

Creates new JavaScript objects.

ex: JS.new User, ["first_name", "last_name"]

throw(term)

Throws the term given

typeof(value)

Returns the type of the given value

update(object, map)

Updates an existing JavaScript object.

ex: JS.update elem, %{“width” => 100}