Stop being Perfect

Stop being Perfect

Presented at Web Rebels, Oslo, Norway
May 22nd 2014

Angus Croll

May 22, 2014

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

100 slides
  1. I’m British but I live in America I work at

    Twitter (used by twitter.com, tweetdeck, airbnb, beats music etc.) I co-wrote Flight
  2. var Widget = function(color) { this.color = color; }; Widget.prototype.render

    = function() { console.log('render...', 'width:', this.width, 'height:', this.height, 'color:', this.color); } var Button = function(width, height, cb, label, color) { this.width = width; this.height = height; this.cb = cb; this.label = label; Widget.call(this, color); }; Button.prototype = new Widget(); Button.prototype.constructor = Button; Button.prototype.initialize = function() { console.log('init...', 'onclick:', this.cb, 'label:', this.label); }; var b = new Button(20, 30, alert, 'hello', 'blue') b.initialize(); b.render();
  3. var widget = { render : function() { console.log('render...', 'width:',

    this.width, 'height:', this.height, 'color:', this.color); } } var button = { initialize: function() { console.log('init...', 'onclick:', this.cb, 'label:', this.label); widget.render.call(this); } } var myButton = { width:20, height:30, cb:alert, label:'hello', color:'blue'}; button.initialize.call(myButton);
  4. var widget = { render : function() { console.log('render...', 'width:',

    this.width, 'height:', this.height, 'color:', this.color); } } var button = { initialize: function() { console.log('init...', 'onclick:', this.cb, 'label:', this.label); widget.render.call(this); } } var myButton = { width:20, height:30, cb:alert, label:'hello', color:'blue'}; button.initialize.call(myButton);
  5. var widget = { render : function(width, height, color) {

    console.log('render...', 'width:', width, 'height:', height, 'color:', color); } } var button = { initialize: function(cb, label, width, height, color) { console.log('init...', 'onclick:', cb, 'label:', label); widget.render(width, height, color); } } button.initialize(20, 30, alert, 'hello', 'blue');
  6. var widget = { render : function(width, height, color) {

    console.log('render...', 'width:', width, 'height:', height, 'color:', color); } } var button = { initialize: function(cb, label, width, height, color) { console.log('init...', 'onclick:', cb, 'label:', label); widget.render(width, height, color); } } button.initialize(20, 30, alert, 'hello', 'blue');
  7. “Only hasOwnProperty will give the correct and expected result; this

    is essential when iterating over the properties of any object” —JavaScript Garden
  8. var obj = Object.create(null); obj.a = 3; obj.b = 5;

    for (var p in obj) { if (obj.hasOwnProperty(p)) { console.log(obj[p]); } }
  9. var obj = Object.create(null); obj.a = 3; obj.b = 5;

    for (var p in obj) { if (obj.hasOwnProperty(p)) { console.log(obj[p]); } }
  10. var obj = Object.create(null); obj.a = 3; obj.b = 5;

    for (var p in obj) { if (obj.hasOwnProperty(p)) { console.log(obj[p]); } } TypeError: [Object Object] has no method hasOwnProperty
  11. //From prototype.js extension of //String.prototype function times(count) { return count

    < 1 ? '' : new Array(count + 1).join(this); } 'me'.times(10); //"memememememememememe"
  12. //using Ramanujan's approximation function factorial(n){ with(Math) { var r =

    sqrt(PI)*pow(n/E,n); r *= pow(8*pow(n, 3) + 4*(n*n) + n + 1/30, 1/6); return r; } }
  13. //using Ramanujan's approximation function factorial(n){ with(Math) { var r =

    sqrt(PI)*pow(n/E,n); r *= pow(8*pow(n, 3) + 4*(n*n) + n + 1/30, 1/6); return r; } }
  14. var addHandlers = function(nodes) { for (var i = 0;

    i < nodes.length; i++) { nodes[i].onclick = function(e) {alert(i);} } };
  15. var addHandlers = function(nodes) { for (var i = 0;

    i < nodes.length; i++) { nodes[i].onclick = function(i) { return function(e) {alert(i)}; }(i); } };
  16. var addHandlers = function(nodes) { for (var i = 0;

    i < nodes.length; i++) { with ({i:i}) { nodes[i].onclick = function(e) {alert(i)} } } };
  17. var addHandlers = function(nodes) { for (var i = 0;

    i < nodes.length; i++) { nodes[i].onclick = function(e) {alert(i);} } };
  18. var addHandlers = function(nodes) { for (var i = 0;

    i < nodes.length; console.log(i),i++) { nodes[i].onclick = function(e) {alert(i);} } };
  19. var addHandlers = function(nodes) { for (var i = 0;

    i < nodes.length; console.log(i),i++) { nodes[i].onclick = function(e) {alert(i);} } };
  20. //see http://perfectionkills.com/global-eval-what- are-the-options/ by Kangax (function() { var localStorage =

    "bwahahahaha!" return (0,eval)("localStorage") })(); //"Storage {length: 0}"
  21. “use [] not new Array” “aren't r and i also

    global?” “eval.....” “global arr variable :P” “Why is it Useful?” “evil() is like cheating” “no global functions!”
  22. me

  23. Your code will Break - it always does. Keep it

    simple and human readable so it’s easier to fix.
  24. Picture Credits Mr Perfect by Roger Hargreaves http://www.mrmen.com/characters/mr-perfect/index.html If Hemingway

    wrote JavaScript by Angus Croll No Starch Press Bayeux Tapestry Maker http://bayeux.datensalat.net The Scream by Edvard Munch JavaScript: Good Parts by Douglas Crockford http://shop.oreilly.com/product/9780596517748.do cheetah cubs 2013bestpicz.blogspot.co.uk lion cubs https://c2.staticflickr.com/6/5339/6947068456_0b99b8fa58_z.jpg

More Decks by Angus Croll

Other Decks in Technology