Cameron McCormack · @heycam
…there was the DOM.
interface Node {
const unsigned short ELEMENT_NODE = 1;
const unsigned short ATTRIBUTE_NODE = 2;
...
readonly attribute DOMString nodeName;
attribute DOMString nodeValue;
...
Node appendChild(in Node newChild) raises(DOMException);
...
};
interface Node {
const unsigned short ELEMENT_NODE = 1;
const unsigned short ATTRIBUTE_NODE = 2;
...
readonly attribute DOMString nodeName;
attribute DOMString nodeValue;
...
Node appendChild(in Node newChild) raises(DOMException);
...
};
interface Node {
const unsigned short ELEMENT_NODE = 1; // constants
const unsigned short ATTRIBUTE_NODE = 2;
...
readonly attribute DOMString nodeName;
attribute DOMString nodeValue;
...
Node appendChild(in Node newChild) raises(DOMException);
...
};
interface Node {
const unsigned short ELEMENT_NODE = 1;
const unsigned short ATTRIBUTE_NODE = 2;
...
readonly attribute DOMString nodeName; // attributes
attribute DOMString nodeValue;
...
Node appendChild(in Node newChild) raises(DOMException);
...
};
interface Node {
const unsigned short ELEMENT_NODE = 1;
const unsigned short ATTRIBUTE_NODE = 2;
...
readonly attribute DOMString nodeName;
attribute DOMString nodeValue;
...
Node appendChild(in Node newChild) raises(DOMException);
... // operations
};
Defines a set of C-like types — float, bool, unsigned long, …
undefined
false
, true
0
, -2.5
, Infinity
, NaN
""
, "hello"
{ }
, { "key": "value" }
, [1, 2, 3]
, function() { }
, /^regexp?$/
array_instance → Array.prototype → Object.prototype → null
array_instance = [10, 20, 30]: 0, 1, 2, length
Array.prototype: push, pop, concat, …
Object.prototype: toString, …
ELEMENT_NODE
property be on both Node
and Node.prototype
?anElement.nodeValue = 0x10;
anElement.nodeValue = ({ "toString": function() { return "a"; } });
Object.getOwnPropertyDescriptor()
.({ "nodeType": Node.ELEMENT_NODE })
to it?nodesubclass_instance → NodeSubclass → Node → Object
function NodeSubclass() { }
NodeSubclass.prototype.__proto__ = Node.prototype;
document.documentElement.appendChild(new NodeSubclass());
Any JS value is converted to an IDL DOMString value by effectively passing it to the globalanElement.nodeValue = 0x10;
anElement.nodeValue = ({ "toString": function() { return "a"; } });
String()
function.
anElement.nodeValue = "16";
anElement.nodeValue = "a";
Author created JS objects will never be considered to implement an interface such as Node. A TypeError exception will be thrown.var nodeLike = ({ "nodeType": Node.ELEMENT_NODE });
document.documentElement.appendChild(nodeLike)
function NodeSubclass() { }
NodeSubclass.prototype.__proto__ = Node.prototype;
document.documentElement.appendChild(new NodeSubclass());
Node
and Node.prototype
.var kids = document.documentElement.childNodes;
alert(kids); // [object NodeList]
alert(kids[0]); // [object HTMLHeadElement]
alert(kids[1]); // [object HTMLBodyElement]
interface NodeList {
getter Node? item(unsigned long index);
readonly attribute unsigned long length;
};
[Constructor]
extended attribute declares that you can use new
on the interface:
Then:[Constructor]
interface Document : Node {
readonly attribute Element? documentElement;
...
};
var doc = new Document();
[Constructor(DOMString type, optional EventInit eventInitDict)]
interface Event {
void initEvent(DOMString type, boolean bubbles, boolean cancelable);
...
};
dictionary EventInit {
boolean bubbles;
boolean cancelable;
};
you can write:var event = document.createEvent("Event");
event.initEvent("click", true, false);
var event1 = new Event("click", { bubbles: true, cancelable: false });
var event2 = new Event("click", { bubbles: true });
var event3 = new Event("click");
Then:enum CanvasDrawingStylesFillRules { "nonzero", "evenodd" };
interface CanvasRenderingContext2D {
attribute CanvasDrawingStylesFillRules fillRule;
...
};
var ctx = document.querySelector("canvas").getContext("2d");
ctx.fillRule = "evenodd";
ctx.fillRule = "something"; // throws TypeError
Firefox 18 | Firefox 21 | ||
---|---|---|---|
DOM Attributes | 695.10runs/s ±1.13% | 814.70runs/s ±0.52% | 15~19% |
DOM Modification | 353.90runs/s ±1.86% | 365.06runs/s ±2.66% | ~same |
DOM Query | 18301.34runs/s ±1.27% | 19598.90runs/s ±1.75% | 4~10% |
DOM Traversal | 504.14runs/s ±0.51% | 671.90runs/s ±0.30% | 32~34% |
Cameron McCormack, Mozilla · @heycam