Element Storage
Associate arbitrary
metadata with an element
MooTools
Prototype
jQuery
store, retrieve,
eliminate
store, retrieve, purge,
getStorage
data, removeData, hasData
Slide 6
Slide 6 text
function isSupported() {
var element = document.createElement("div");
return typeof element.uniqueNumber == "number" &&
typeof document.documentElement.uniqueNumber == "number" &&
element.uniqueNumber != document.documentElement.uniqueNumber;
}
var Storage = {};
Index data by unique element identi er
Slide 7
Slide 7 text
var UID = 2;
function identify(element) {
if (element == window) {
return 0;
} else if (element.nodeType == 9) {
return 1;
} else if (element.nodeType == 1) {
if (!/^(?:embed|object|applet)$/i.test(element.nodeName)) {
var property = (isSupported() ? "unique" :
"sail") + "Number";
return element[property] || (element[property] = UID++);
}
}
return null;
}
Slide 8
Slide 8 text
The Perils of
Generality
Tailored solutions are the answer
Attributes
Many of these attributes and properties
are deprecated...but browsers still support
them.
If you’re writing a general solution, you’ve
committed yourself to supporting them
as well.
Slide 12
Slide 12 text
Considerations
Opportunity cost
Slide 13
Slide 13 text
File size
Complexity
Performance
Micro-Libraries
General solutions
Edge cases
Slide 14
Slide 14 text
Targeted, low-level utility libraries
Stop writing library plug-ins
General libraries are abstraction layers with a different syntax
Don’t aim for identical experiences
across every browser
Learn and target DOM APIs instead
Save cross-browser consistency for
low-level methods
Slide 15
Slide 15 text
“Any change to a low-level abstraction
propagates to its dependencies.”
“Before attempting to x any bug, the
author must rst get an
understanding of the problems
caused by their code.”
— Garrett Smith
Slide 16
Slide 16 text
Accept constraints
Think about what you need
Reduce abstractions
Write only what you need