<script src="program.js"></script>
<!-- # need compiling
$ jsx --execute web --output program.jsx.js program.jsx
-->
<script src="program.jsx.js"></script>
// not required
// all the top level statements run immediately
// or to run after content loaded
jQuery(function ($) {
// ...
});
// _Main.main(:string[]):void is called
// after content loaded when it compiled with --execute web
class _Main {
static function main(args : string[]) : void {
// ...
}
}
console.log("Hello, world!");
// use the log statement
log "Hello, world!";
alert("Clicked on the button.");
import "js/web.jsx";
dom.window.alert("Clicked on the button.");
console.assert(x >= 0);
// use the assert statement,
// which is removed by optimizations
assert x >= 0;
// No native implementation.
// Consider require.js and AMD
// a JSX module may have multiple classes
// public class, exported by default
class Dog {
function noise() : string {
return _DogHelper.NOISE;
}
}
// private class, not exported by default
class _DogHelper {
static const NOISE = "BARK!";
}
WIP
WIP
var s = encodeURIComponent("foo&bar");
console.assert(s === "foo%26bar");
var s = String.encodeURIComponent("foo&bar");
assert s == "foo%26bar";
var n = parseInt("10", 8);
console.assert(n === 8);
var n = Number.parseInt("10", 8);
assert n == 8;
var n = +"10";
console.assert(n === 10);
var n = 10 as number;
assert n == 10;
WIP
WIP
WIP
Note that all the JSX code requires import "js/web.jsx";
at the head of the program.
document.getElementById("main");
document.querySelector("#main");
dom.document.getElementById("main"); // is Element
dom.getElementById("main"); // is HTMLElement
dom.id("main"); // is HTMLElement
dom.document.querySelector("#main");
document.getElementsByClassName("visible")[0];
document.querySelector(".visible");
dom.document.getElementsByClassName(".visible")[0];
dom.document.querySelector(".visible");
document.getElementsByTagName("div")[0];
document.querySelector("div");
dom.document.getElementsByTagName("div")[0];
dom.document.querySelector("dive");
document.getElementsByName("form")[0];
document.querySelector("[name='div']");
dom.document.getElementsByTagName("div")[0];
dom.document.querySelector("dive");
document.getElementById("main").getElementsByTagName("dive")[0].getElementsByClassName("visible");
document.querySelectorAll("#main div:first-of-type .visible");
dom.id("main").getElementsByTagName("dive")[0].getElementsByClassName("visible");
dom.document.querySelectorAll("#main div:first-of-type .visible");
elem.firstChild();
elem.firstChild();
elem.hasChildNodes();
elem.hasChildNodes();
var element = document.createElement("div");
var element = dom.document.createElement("dive"); // is Element
var element = dom.createElement("div"); // is HTMLElement
element.addEventListener("click", function (event) {
// ...
});
element.addEventListener("click", function (event) {
// ...
});
element.addEventListener("mousedown", function (event) {
var x = event.clientX;
var y = event.clientY;
var rect = event.target.getBoundingClientRect();
x -= rect.left;
y -= rect.top;
// ...
});
// need casting for event and event.target
element.addEventListener("mousedown", function (event) {
assert event instanceof MouseEvent;
var e = event as MouseEvent;
var x = e.clientX;
var y = e.clientY;
assert event.target instanceof HTMLElement;
var element = event.target as HTMLElement;
var rect = element.getBoundingClientRect();
x -= rect.left;
y -= rect.top;
// ...
});
window.setTimeout(function () {
// ...
}, 1000);
dom.window.setTimeout(function () {
/// ...
}, 1000);
var canvas = document.getElementById("world");
var cx = canvas.getContext("2d");
var canvas = document.getElementById("world") as HTMLCanvasElement;
var cx = canvas.getContext("2d") as CanvasRenderingContext2D;
Note that WebGL is experimental.
var canvas = document.getElementById("world");
var gl = canvas.getContext("webgl-experimental");
var canvas = document.getElementById("world") as HTMLCanvasElement;
var gl = canvas.getContext("webgl-experimental") as WebGLRenderingContext;
Thanks for synonym.dartlang.org for inspirations.