Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript Anti-Patterns: Moving from Java to J...

JavaScript Anti-Patterns: Moving from Java to JavaScript

Short talk detailing five common issues new JavaScript encounter when moving from Java.

James Thomas

May 02, 2012
Tweet

More Decks by James Thomas

Other Decks in Technology

Transcript

  1. “JavaScript has two sets of equality operators: === and !==,

    and their evil twins == and !=” D. Crockford - “JavaScript: The Good Parts” Wednesday, 2 May 2012
  2. '' == '0' // false 0 == '' // true

    0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true ' \t\r\n ' == 0 // true Wednesday, 2 May 2012
  3. function addLinks () { for (var i=0, link; i<5; i++)

    { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function () { alert(i); }; document.body.appendChild(link); } } Wednesday, 2 May 2012
  4. “...unlike C, C++, and Java, JavaScript does not have block-level

    scope. All variables declared in a function, no matter where they are declared, are defined throughout the function.” D. Flanagan “JavaScript: The Definitive Guide” Wednesday, 2 May 2012
  5. function addLinks () { var i; for (i=0, link; i<5;

    i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function () { alert(i); }; document.body.appendChild(link); } } Wednesday, 2 May 2012
  6. function addLinks () { for (var i=0, link; i<5; i++)

    { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function (num) { return function () { alert(num); } }(i); document.body.appendChild(link); } } Wednesday, 2 May 2012
  7. messages = []; var elem = document.getElementById("messages"); elem.onclick = function

    () { var field = document.getElementById("input"); messages.push(field.value); }; Wednesday, 2 May 2012
  8. Why Global Variables Should Be Avoided When Unnecessary... • Non-locality

    • No Access Control or Constraint Checking • Implicit coupling • Concurrency issues • Namespace pollution • Memory allocation issues • Testing and Confinement http://c2.com/cgi/wiki?GlobalVariablesAreBad Wednesday, 2 May 2012
  9. var messages = []; var elem = document.getElementById("messages"); var onclick

    = function (messages) { return function () { var field = document.getElementById("input"); messages.push(field.value); }; }; elem.onclick = onclick(messages); Wednesday, 2 May 2012
  10. function PersonDetails() { } PersonDetails.prototype = {}; PersonDetails.prototype.setAge = function

    (age) { this.age = age; }; PersonDetails.prototype.getAge = function () { return this.age; }; PersonDetails.prototype.setName = function (name) { this.name = name; }; PersonDetails.prototype.getName = function () { return this.name; }; var details = new PersonDetails(); details.setAge(28); details.setName("James Thomas"); updateDetailsForPerson(details); Wednesday, 2 May 2012
  11. function addClasses(element, classes) { for (var i = 0; i

    < classes.length; i++) { element.className += " " + classes[i]; } } Wednesday, 2 May 2012
  12. “Reflow is the name of the web browser process for

    re-calculating the positions and geometries of elements in the document, for the purpose of re-rendering part or all of the document.” https://developers.google.com/speed/articles/reflow Wednesday, 2 May 2012
  13. “Reflows are very expensive in terms of performance, and is

    one of the main causes of slow DOM scripts.” http://dev.opera.com/articles/view/efficient-javascript/?page=3#reflow Wednesday, 2 May 2012
  14. http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ •Adding, removing, updating DOM nodes •Hiding a DOM node

    with display: none (reflow and repaint) or visibility: hidden (repaint only, because no geometry changes) •Moving, animating a DOM node on the page •Adding a stylesheet, tweaking style properties •User action such as resizing the window, changing the font size or scrolling Wednesday, 2 May 2012
  15. function addClasses(element, classes) { var newClasses = ""; for (var

    i = 0; i < classes.length; i++) { newClasses += " " + classes[i]; } element.className += newClasses; } Wednesday, 2 May 2012