mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-14 23:09:22 +00:00
documentation for $map (and more on $get, $set, $delete)
This commit is contained in:
parent
06e3ce8b64
commit
990e50ac7f
@ -16,8 +16,8 @@ Content
|
||||
- [Core types and objects](#core-types-and-objects)
|
||||
- [Classes](#classes)
|
||||
- [Interfaces](#interfaces)
|
||||
- [Untyped objects (maps)](#untyped-objects-maps)
|
||||
- [Enums](#enums)
|
||||
- [Indexed objects](#indexed-objects)
|
||||
- [Globals](#globals)
|
||||
- [Optional parameters](#optional-parameters)
|
||||
- [Bridging to external JavaScript elements](#bridging-to-external-javascript-elements)
|
||||
@ -379,6 +379,77 @@ In JavaScript, objects can have properties and functions, but can also (not excl
|
||||
|
||||
- `$new` is used to state that the object can be used as a constructor.
|
||||
|
||||
### Untyped objects (maps)
|
||||
|
||||
In JavaScript, object can be seen as maps containing key-value pairs (key is often called *index*, especially when it is a number). So, in JSweet, all objects define the special functions (defined on `jsweet.lang.Object`):
|
||||
|
||||
- `$get(key)` accesses a value with the given key.
|
||||
|
||||
- `$set(key,value)` sets or replace a value for the given key.
|
||||
|
||||
- `$delete(key)` deletes the value for the given key.
|
||||
|
||||
#### Reflective/untyped accesses
|
||||
|
||||
The functions `$get(key)`, `$set(key,value)` and `$delete(key)` can be seen as a simple reflective API to access object fields and state. Note also the static method `jsweet.lang.Object.keys(object)`, which returns all the keys defined on a given object.
|
||||
|
||||
The following code uses this API to introspect the state of an object `o`.
|
||||
|
||||
``` java
|
||||
for(String key : jsweet.lang.Object.keys(o)) {
|
||||
console.log("key=" + key + " value=" + o.$get(key));
|
||||
});
|
||||
```
|
||||
|
||||
When not having the typed API of a given object, this API can be useful to manipulate the object in an untyped way (of course it should be avoided as much as possible).
|
||||
|
||||
#### Untyped objects initialization
|
||||
|
||||
One can use the `$set(key,value)` function to create new untyped object. For instance:
|
||||
|
||||
``` java
|
||||
Object point = new jsweet.lang.Object() {{ $set("x", 1); $set("y", 1); }};
|
||||
```
|
||||
|
||||
As a shortcut, one can use the `jsweet.util.Global.$map` function:
|
||||
|
||||
``` java
|
||||
import static jsweet.util.Global.$map;
|
||||
[...]
|
||||
Object point = $map("x", 1, "y", 1);
|
||||
```
|
||||
|
||||
#### Indexed objects
|
||||
|
||||
The type of keys and values can be overloaded for every object. For example, the `Array<T>` class, will define keys as numbers and values as objects conforming to type `T`.
|
||||
|
||||
In the case of objects indexed with number keys, it is allowed to implement the `java.lang.Iterable` interface so that it is possible to use they in *foreach* loops. For instance, the `NodeList` type (from the DOM) defines an indexed function:
|
||||
|
||||
``` java
|
||||
@Interface
|
||||
class NodeList implements java.lang.Iterable {
|
||||
public double length;
|
||||
public Node item(double index);
|
||||
public Node $get(double index);
|
||||
}
|
||||
```
|
||||
|
||||
In JSweet, you can access the node list elements with the `$get` function, and you can also iterate with the *foreach* syntax. The following code generates fully valid JavaScript code.
|
||||
|
||||
``` java
|
||||
NodeList nodes = ...
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
HTMLElement element = (HTMLElement) nodes.$get(i);
|
||||
[...]
|
||||
}
|
||||
// same as:
|
||||
NodeList nodes = ...
|
||||
for (Node node : nodes) {
|
||||
HTMLElement element = (HTMLElement) node;
|
||||
[...]
|
||||
}
|
||||
```
|
||||
|
||||
### Enums
|
||||
|
||||
JSweet allows the definition of enums similarly to Java, but with some restrictions. The following code declares an enum with tree possible values (`A`, `B`, and `C`).
|
||||
@ -424,45 +495,6 @@ public enum WrongConstructsInEnums {
|
||||
}
|
||||
```
|
||||
|
||||
### Indexed objects
|
||||
|
||||
In JavaScript, object can be seen as maps containing key-value pairs (key is often called *index*, especially when it is a number). So, in JSweet, all objects define the special functions (defined on `jsweet.lang.Object`):
|
||||
|
||||
- `$get(key)` accesses a value with the given key.
|
||||
|
||||
- `$set(key,value)` sets or replace a value for the given key.
|
||||
|
||||
- `$delete(key)` deletes the value for the given key.
|
||||
|
||||
The type of keys and values can be overloaded for every object. For example, the `Array<T>` class, will define keys as numbers and values as objects conforming to type `T`.
|
||||
|
||||
In the case of objects indexed with number keys, it is allowed to implement the `java.lang.Iterable` interface so that it is possible to use they in *foreach* loops. For instance, the `NodeList` type (from the DOM) defines an indexed function:
|
||||
|
||||
``` java
|
||||
@Interface
|
||||
class NodeList implements java.lang.Iterable {
|
||||
public double length;
|
||||
public Node item(double index);
|
||||
public Node $get(double index);
|
||||
}
|
||||
```
|
||||
|
||||
In JSweet, you can access the node list elements with the `$get` function, and you can also iterate with the *foreach* syntax. The following code generates fully valid JavaScript code.
|
||||
|
||||
``` java
|
||||
NodeList nodes = ...
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
HTMLElement element = (HTMLElement) nodes.$get(i);
|
||||
[...]
|
||||
}
|
||||
// same as:
|
||||
NodeList nodes = ...
|
||||
for (Node node : nodes) {
|
||||
HTMLElement element = (HTMLElement) node;
|
||||
[...]
|
||||
}
|
||||
```
|
||||
|
||||
### Globals
|
||||
|
||||
In Java, on contrary to JavaScript, there is no such thing as global variables or functions (there are only static members, but even those must belong to a class). Thus, JSweet introduces reserved `Globals` classes and `globals` packages. These have two purposes:
|
||||
|
||||
@ -399,6 +399,77 @@ In JavaScript, objects can have properties and functions, but can also (not excl
|
||||
\item \texttt{\$new} is used to state that the object can be used as a constructor.
|
||||
\end{itemize}
|
||||
|
||||
\section{Untyped objects (maps)}
|
||||
|
||||
In JavaScript, object can be seen as maps containing key-value pairs (key is often called \emph{index}, especially when it is a number). So, in JSweet, all objects define the special functions (defined on \texttt{jsweet.lang.Object}):
|
||||
|
||||
\begin{itemize}
|
||||
\item \texttt{\$get(key)} accesses a value with the given key.
|
||||
\item \texttt{\$set(key,value)} sets or replace a value for the given key.
|
||||
\item \texttt{\$delete(key)} deletes the value for the given key.
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Reflective/untyped accesses}
|
||||
|
||||
The functions \texttt{\$get(key)}, \texttt{\$set(key,value)} and \texttt{\$delete(key)} can be seen as a simple reflective API to access object fields and state. Note also the static method \texttt{jsweet.lang.Object.keys(object)}, which returns all the keys defined on a given object.
|
||||
|
||||
The following code uses this API to introspect the state of an object \texttt{o}.
|
||||
|
||||
\begin{lstlisting}[language=Java]
|
||||
for(String key : jsweet.lang.Object.keys(o)) {
|
||||
console.log("key=" + key + " value=" + o.$get(key));
|
||||
});
|
||||
\end{lstlisting}
|
||||
|
||||
When not having the typed API of a given object, this API can be useful to manipulate the object in an untyped way (of course it should be avoided as much as possible).
|
||||
|
||||
\subsection{Untyped objects initialization}
|
||||
|
||||
One can use the \texttt{\$set(key,value)} function to create new untyped object. For instance:
|
||||
|
||||
\begin{lstlisting}[language=Java]
|
||||
Object point = new jsweet.lang.Object() {{ $set("x", 1); $set("y", 1); }};
|
||||
\end{lstlisting}
|
||||
|
||||
As a shortcut, one can use the \texttt{jsweet.util.Global.\$map} function:
|
||||
|
||||
\begin{lstlisting}[language=Java]
|
||||
import static jsweet.util.Global.$map;
|
||||
[...]
|
||||
Object point = $map("x", 1, "y", 1);
|
||||
\end{lstlisting}
|
||||
|
||||
\subsection{Indexed objects}
|
||||
|
||||
The type of keys and values can be overloaded for every object. For example, the \texttt{Array<T>} class, will define keys as numbers and values as objects conforming to type \texttt{T}.
|
||||
|
||||
In the case of objects indexed with number keys, it is allowed to implement the \texttt{java.lang.Iterable} interface so that it is possible to use they in \emph{foreach} loops. For instance, the \texttt{NodeList} type (from the DOM) defines an indexed function:
|
||||
|
||||
\begin{lstlisting}[language=Java]
|
||||
@Interface
|
||||
class NodeList implements java.lang.Iterable {
|
||||
public double length;
|
||||
public Node item(double index);
|
||||
public Node $get(double index);
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
In JSweet, you can access the node list elements with the \texttt{\$get} function, and you can also iterate with the \emph{foreach} syntax. The following code generates fully valid JavaScript code.
|
||||
|
||||
\begin{lstlisting}[language=Java]
|
||||
NodeList nodes = ...
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
HTMLElement element = (HTMLElement) nodes.$get(i);
|
||||
[...]
|
||||
}
|
||||
// same as:
|
||||
NodeList nodes = ...
|
||||
for (Node node : nodes) {
|
||||
HTMLElement element = (HTMLElement) node;
|
||||
[...]
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\section{Enums}
|
||||
|
||||
JSweet allows the definition of enums similarly to Java, but with some restrictions. The following code declares an enum with tree possible values (\texttt{A}, \texttt{B}, and \texttt{C}).
|
||||
@ -444,45 +515,6 @@ public enum WrongConstructsInEnums {
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\section{Indexed objects}
|
||||
|
||||
In JavaScript, object can be seen as maps containing key-value pairs (key is often called \emph{index}, especially when it is a number). So, in JSweet, all objects define the special functions (defined on \texttt{jsweet.lang.Object}):
|
||||
|
||||
\begin{itemize}
|
||||
\item \texttt{\$get(key)} accesses a value with the given key.
|
||||
\item \texttt{\$set(key,value)} sets or replace a value for the given key.
|
||||
\item \texttt{\$delete(key)} deletes the value for the given key.
|
||||
\end{itemize}
|
||||
|
||||
The type of keys and values can be overloaded for every object. For example, the \texttt{Array<T>} class, will define keys as numbers and values as objects conforming to type \texttt{T}.
|
||||
|
||||
In the case of objects indexed with number keys, it is allowed to implement the \texttt{java.lang.Iterable} interface so that it is possible to use they in \emph{foreach} loops. For instance, the \texttt{NodeList} type (from the DOM) defines an indexed function:
|
||||
|
||||
\begin{lstlisting}[language=Java]
|
||||
@Interface
|
||||
class NodeList implements java.lang.Iterable {
|
||||
public double length;
|
||||
public Node item(double index);
|
||||
public Node $get(double index);
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
In JSweet, you can access the node list elements with the \texttt{\$get} function, and you can also iterate with the \emph{foreach} syntax. The following code generates fully valid JavaScript code.
|
||||
|
||||
\begin{lstlisting}[language=Java]
|
||||
NodeList nodes = ...
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
HTMLElement element = (HTMLElement) nodes.$get(i);
|
||||
[...]
|
||||
}
|
||||
// same as:
|
||||
NodeList nodes = ...
|
||||
for (Node node : nodes) {
|
||||
HTMLElement element = (HTMLElement) node;
|
||||
[...]
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\section{Globals}
|
||||
|
||||
In Java, on contrary to JavaScript, there is no such thing as global variables or functions (there are only static members, but even those must belong to a class). Thus, JSweet introduces reserved \texttt{Globals} classes and \texttt{globals} packages. These have two purposes:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user