SlideShare una empresa de Scribd logo
1 de 92
Descargar para leer sin conexión
Defsi,
cro-brows codg
       wh
        .
    prototype
        .
ialm




aly
yr co




MSIE
T fi ag 
 JavaScript grief
#1:
“My co wks  Fifox, but
  fails eryw else…”
        (peciay  IE)
Fifox s Fibug,
      crse
Safi s
Web Inspecr
 & Drosa
Opa s
         Dragfly
hp://w.opa.com/products/dragfly/
Inet Expl s…
  ★   Vual Stud
  ★   Microst Script Ed
  ★   Microst Script Debu
Ty a...
★   Suck
Debug regi 
     IE 6–7:
a.
Do “fmal” bug if
    ’s yr g
b.
Use a JavaScript s 
get  tac prompt
Jh
hp://w.biyg.com/jh/
c.
Use MS Script Debu/
Ed  get ack trac
d.
 Do old-fhn
“a” bug
#2:
“OK, my co wks  IE now,
      but  slow 
counicg by tegraph.”
Plu:
Use  Fibug prir
Pr-m’s prilg:
msu elaps me betwn
   y two co pots
var Timer = Class.create({
  initialize: function() {
     this.startTime = new Date().valueOf();
  },

  end: function() {
    var endTime = new Date().valueOf();
    return (this.startTime - endTime).toString();
  }
});
function doSomethingCostly() {
  var timer1 = new Timer();

    // do something that takes forever...

    alert(timer1.end());
}
Ce udy:
zebra-ripg a tab
Two tts:
1 rows  10 rows
Revn 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;);
  }
});
1 Rows   10 Rows


Safi     14ms     603ms

Fifox   26ms      367ms

  IE     341ms     5268ms
:-(
Revn 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;);
  }
});
1 Rows   10 Rows


Safi     12ms      123ms

Fifox    14ms      130ms

  IE     160ms     3245ms
:-(
Revn 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;);
  }
});
1 Rows   10 Rows


Safi     5ms       53ms

Fifox    13ms      143ms

  IE     160ms     31ms
:-(
Revn 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;);
  }
});
1 Rows   10 Rows


Safi     1ms        8ms

Fifox    6ms       64ms

  IE     20ms       141ms
HOLY CRAP
So wt did we n?
Ls 1:
 Secr/$$ e opmiz, but
wh gh nos   page,
   y’ brg   s kns
Ls 2:
“Extdg” emts  slow,
  do not sca  lge
     no sult sets
$  Emt.extd e 
shrk-wrap f syacc sug
In morn browss,
Emt.extd do nog; 
 IE 6–7  s  copy ce
       meods muay
A Protype DOM
      lps use
Emt.extd tnay
Wa —   sayg
“D’t use Protype?”
We, y:
  Rch tsi
 yr abrac
wn ’s o coly
Ls #3:
A lt b  opmizn
    go a lg way
A few l  n-
Protype co, wrt
 ce, c mn huge
  pfmce gas
As always,
opmizn si 
 lps pays f big
Ls 4:
Once  “fls” ft… op!
#3:
     “Now ’s py ft  IE, but
if I a my page op wn I go 
     lunch, IE’s froz by  me
               I get back.”
Memy aks
Not ju IE 6.0;
IE 7.0 claims  fix
 m, but ’s a li
Y c’t be su yr co
  ak-fe uil y try 
       al wld
Framewks c lp
 a lt, but ’s i
yr probm  sol
Drip
hp://fnwe.com/ieak/
Load yr page; wch
memy usage ce
     (’s nmal)
Now load  page; if
memy usage c
   aga, ’s bad
A y seg expo
  no prop 
fce n-primis?
  ( “circul fce” ak)
A y signg es
 tsi  Protype’s
     e APIs?
   (e.g., emt.click)
(Protype’s e syem wi
 c up aft self, but if y
d’t use , y’  yr own)
A y cag Emt.place
  Emt.upde  ctt
   wh tacd lts?
If so, tach 
  lts fir
foo.stopObserving(quot;clickquot;, respondToFoo);
// (detaches the respondToFoo listener)

foo.stopObserving(quot;clickquot;);
// (detaches all click listeners on foo)

foo.stopObserving();
// (detaches _all_ listeners on foo)
Protype 1.6.[next]
wi lp y t wh 
 FAL TT:
L IE rug
  ornight
#4:
“My a pfms wrfuy
 now, but my co lks like
     hum suffg.”
In or wds:
“How do I kp my co
mataab if  s 
  a f a ese
    special c?”
Wt if we cld kp
 a  ugly cks
    e place?
Func#wrap
Origal func

function getOffsetTop(element) {
  return element.offsetTop;
}
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);
  }
);
Only IE nds  s 
   modifi rsn
Y c kp 
“wra” funcs 
     e place
<script src=quot;js/prototype.jsquot;
 type=quot;text/javascriptquot;></script>
<script src=quot;js/main.jsquot;
 type=quot;text/javascriptquot;></script>
<!--[if IE]>
  <script src=quot;js/ie-only.jsquot;
   type=quot;text/javascriptquot;></script>
<![endif]-->
BUT:
L a lpful cot
// NOTE: Redefined in ie-only.js
function getOffsetTop(element) {
  return element.offsetTop;
}
 ALSO:
Expla why y nd
   wk
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);
  }
);
In or wds,
co   d...
…n wre co
adapts/dgs  do
  quid msagg
#5:
“G! My co lks  gd
  I fl. How do I avoid er
  vg  do  aga?”
EAT YR GETABS
  (wre un tts &
   funcal tts)
“Oh, I’d lo , but I wk   UI lay,
     y c’t ay un-tt 
tface… , yway, we e way
        o rapidly  do tt-drin
lopmt… but y’ right, I’ tay
 wre a ful  tts  sn  I get
              some me…”
SPE ME YR
  EXCUS
Wre un tts f
eryg  c be
    aum
Protype do ;
script.aculo.us do ;
  y c do , o
Wre funcal tts 
   make  si f
hums  tt  
QA wi buy y a py
Wre “diy” tts
(row curbas
   yr co)
Wt abt Senium?
        WIR?
(cd  play hum put)
meh.
Qus?

hp://wdupt.net
 hp://protypejs.g

Más contenido relacionado

La actualidad más candente

Los numeros reales
Los numeros realesLos numeros reales
Los numeros reales
Karina Ruiz
 
Лекц 11
Лекц 11Лекц 11
Лекц 11
Muuluu
 
Bai Giang 8
Bai Giang 8Bai Giang 8
Bai Giang 8
nbb3i
 
Иван Калинин - Как правильно выбрать язык программирования
Иван Калинин - Как правильно выбрать язык программированияИван Калинин - Как правильно выбрать язык программирования
Иван Калинин - Как правильно выбрать язык программирования
Yandex
 

La actualidad más candente (16)

Lekts13
Lekts13Lekts13
Lekts13
 
Bai Tap Vb
Bai Tap VbBai Tap Vb
Bai Tap Vb
 
Los numeros reales
Los numeros realesLos numeros reales
Los numeros reales
 
Лекц 11
Лекц 11Лекц 11
Лекц 11
 
Web design book
Web design bookWeb design book
Web design book
 
Bai Giang 8
Bai Giang 8Bai Giang 8
Bai Giang 8
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
 
창조적 인재로 살아가기 How to Live Creative
창조적 인재로 살아가기 How to Live Creative 창조적 인재로 살아가기 How to Live Creative
창조적 인재로 살아가기 How to Live Creative
 
ბანკები და ფინანსები #446
ბანკები და ფინანსები #446ბანკები და ფინანსები #446
ბანკები და ფინანსები #446
 
JavaScript OOP
JavaScript OOPJavaScript OOP
JavaScript OOP
 
Иван Калинин - Как правильно выбрать язык программирования
Иван Калинин - Как правильно выбрать язык программированияИван Калинин - Как правильно выбрать язык программирования
Иван Калинин - Как правильно выбрать язык программирования
 
Linq
LinqLinq
Linq
 
ბანკები და ფინანსები #447
ბანკები და ფინანსები #447ბანკები და ფინანსები #447
ბანკები და ფინანსები #447
 
ბანკები და ფინანსები #445
ბანკები და ფინანსები #445ბანკები და ფინანსები #445
ბანკები და ფინანსები #445
 
23.06.11
23.06.1123.06.11
23.06.11
 
ბანკები და ფინანსები #466
ბანკები და ფინანსები #466ბანკები და ფინანსები #466
ბანკები და ფინანსები #466
 

Destacado

Ecuaciones de-segundo-grado-23997
Ecuaciones de-segundo-grado-23997Ecuaciones de-segundo-grado-23997
Ecuaciones de-segundo-grado-23997
tom1informatica
 

Destacado (6)

Powerful mostly unknown Javascript-Features
Powerful mostly unknown Javascript-FeaturesPowerful mostly unknown Javascript-Features
Powerful mostly unknown Javascript-Features
 
2008 Steg
2008 Steg2008 Steg
2008 Steg
 
Javascript Performance Tweaks
Javascript Performance TweaksJavascript Performance Tweaks
Javascript Performance Tweaks
 
Ecuaciones de-segundo-grado-23997
Ecuaciones de-segundo-grado-23997Ecuaciones de-segundo-grado-23997
Ecuaciones de-segundo-grado-23997
 
Reviving Creativity
Reviving CreativityReviving Creativity
Reviving Creativity
 
Cloud Computing Presentation for ISVs | SaaS Presentation
Cloud Computing Presentation for ISVs | SaaS PresentationCloud Computing Presentation for ISVs | SaaS Presentation
Cloud Computing Presentation for ISVs | SaaS Presentation
 

Más de Andrew Dupont

Más de Andrew Dupont (6)

Learning new words
Learning new wordsLearning new words
Learning new words
 
How to Argue about Code
How to Argue about CodeHow to Argue about Code
How to Argue about Code
 
How to Argue about JavaScript
How to Argue about JavaScriptHow to Argue about JavaScript
How to Argue about JavaScript
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
 
Open Government: An Overview
Open Government: An OverviewOpen Government: An Overview
Open Government: An Overview
 

Defensive, Cross-Browser Coding with Prototype