Más contenido relacionado La actualidad más candente (16) Defensive, Cross-Browser Coding with Prototype9. Opa s
Dragfly
hp://w.opa.com/products/dragfly/
22. var Timer = Class.create({
initialize: function() {
this.startTime = new Date().valueOf();
},
end: function() {
var endTime = new Date().valueOf();
return (this.startTime - endTime).toString();
}
});
26. Revn 1
var TableStriper = Class.create({
initialize: function(element) {
var timer = new Timer();
this.element = $(element);
this.element.select(quot;tbody > tr:nth-child(even)quot;)
.invoke(quot;addClassNamequot;, quot;row-evenquot;);
this.element.select(quot;tbody > tr:nth-child(odd)quot;)
.invoke(quot;addClassNamequot;, quot;row-oddquot;);
alert(timer.end() + quot;msquot;);
}
});
27. 1 Rows 10 Rows
Safi 14ms 603ms
Fifox 26ms 367ms
IE 341ms 5268ms
29. Revn 2
var TableStriper = Class.create({
initialize: function(element) {
var timer = new Timer();
this.element = $(element);
// Why use two selectors? Use one and loop instead
var trs = this.element.select(quot;tbody > trquot;);
trs.each( function(tr, i) {
tr.addClassName(i % 2 == 0 ?
quot;row-oddquot; : quot;row-evenquot;);
});
alert(timer.end() + quot;msquot;);
}
});
30. 1 Rows 10 Rows
Safi 12ms 123ms
Fifox 14ms 130ms
IE 160ms 3245ms
32. Revn 3
var TableStriper = Class.create({
initialize: function(element) {
var timer = new Timer();
this.element = $(element);
// Eschew CSS selector syntax altogether
var tbodies = this.element.getElementsByTagName(quot;tbodyquot;);
for (var i = 0, tbody, trs; i < tbodies.length; i++) {
tbody = tbodies[i];
trs = tbody.getElementsByTagName(quot;trquot;);
for (var j = 0, tr; j < trs.length; j++) {
tr = trs[j];
// filter out anything that isn't a direct child of the TBODY
if (tr.parentNode !== tbody) continue;
$(tr).addClassName(j % 2 == 0 ? quot;row-oddquot; : quot;row-evenquot;);
}
}
alert(timer.end() + quot;msquot;);
}
});
33. 1 Rows 10 Rows
Safi 5ms 53ms
Fifox 13ms 143ms
IE 160ms 31ms
35. Revn 4
var TableStriper = Class.create({
initialize: function(element) {
var timer = new Timer();
this.element = $(element);
var tbodies = this.element.getElementsByTagName(quot;tbodyquot;);
for (var i = 0, tbody, trs; i < tbodies.length; i++) {
tbody = tbodies[i];
trs = tbody.rows; // there's a pre-defined collection for this
for (var j = 0, tr; j < trs.length; j++) {
tr = trs[j];
// OK, we'll stop quot;extendingquot; elements now
tr.className += j % 2 == 0 ? quot; row-oddquot; : quot; row-evenquot;;
}
}
alert(timer.end() + quot;msquot;);
}
});
36. 1 Rows 10 Rows
Safi 1ms 8ms
Fifox 6ms 64ms
IE 20ms 141ms
44. Wa — sayg
“D’t use Protype?”
45. We, y:
Rch tsi
yr abrac
wn ’s o coly
47. A few l n-
Protype co, wrt
ce, c mn huge
pfmce gas
50. #3:
“Now ’s py ft IE, but
if I a my page op wn I go
lunch, IE’s froz by me
I get back.”
52. Not ju IE 6.0;
IE 7.0 claims fix
m, but ’s a li
53. Y c’t be su yr co
ak-fe uil y try
al wld
57. Now load page; if
memy usage c
aga, ’s bad
58. A y seg expo
no prop
fce n-primis?
( “circul fce” ak)
59. A y signg es
tsi Protype’s
e APIs?
(e.g., emt.click)
61. A y cag Emt.place
Emt.upde ctt
wh tacd lts?
67. In or wds:
“How do I kp my co
mataab if s
a f a ese
special c?”
68. Wt if we cld kp
a ugly cks
e place?
71. Modifi func
getOffsetTop = getOffsetTop.wrap(
function(proceed, element) {
// IE will report incorrect values
// unless we give the element quot;layoutquot;
if (!element.currentStyle.hasLayout == -1)
element.style.zoom = 1;
return proceed(element);
}
);
76. // NOTE: Redefined in ie-only.js
function getOffsetTop(element) {
return element.offsetTop;
}
78. getOffsetTop = getOffsetTop.wrap(
function(proceed, element) {
// IE will report incorrect values
// unless we give the element quot;layoutquot;
if (!element.currentStyle.hasLayout == -1)
element.style.zoom = 1;
return proceed(element);
}
);
83. “Oh, I’d lo , but I wk UI lay,
y c’t ay un-tt
tface… , yway, we e way
o rapidly do tt-drin
lopmt… but y’ right, I’ tay
wre a ful tts sn I get
some me…”