1. Javascript + jQuery: With great power, comes great responsibility http://www.slideshare.net/ciberglo/scti-2011-minicurso-jquery Gabriel Lima @glima5 github.com/gabriellima
27. Scope var iAmGlobal = 5 * 5; function doSomething() { var inner = 5 * 5; }; var g = "global"; function go() { var l = "local"; } go(); console.log(l); // throws a reference error
28. Scope: How it works function go() { console.debug(this); } go(); var myObject = { go: function() { console.debug(this); } }; myObject.go(); // console.debugs a reference to myObject
29. Scope: How it works function MyClass() { this.go = function() { console.debug(this); } }; var instance1 = new MyClass(); var instance2 = new MyClass(); instance1.go(); // console.debugs a reference to the MyClass instance1 instance2.go(); // console.debugs a reference to the MyClass instance2
30.
31. Todo o resto deve ser deixado pra tecnologia que temos pra fazer o serviço.
69. Event handlers should only handle events //the wrong way!!! function handleClick(event){ var popup = document.getElementById("popup"); popup.style.left = event.clientX + "px"; popup.style.top = event.clientY + "px"; popup.className = "reveal"; }
70. Don't pass the event object around //better, but still wrong function handleClick(event){ showPopup(event); } function showPopup(event){ var popup = document.getElementById("popup"); popup.style.left = event.clientX + "px"; popup.style.top = event.clientY + "px"; popup.className = "reveal"; }
71. Separe corretamente os event handlings //fuck yeah!! function handleClick(event){ showPopup(event.clientX, event.clientY); } function showPopup(x, y){ var popup = document.getElementById("popup"); popup.style.left = x + "px"; popup.style.top = y + "px"; popup.className = "reveal"; }
72. Avoid global functions and variables function handleClick(event){ showPopup(event.clientX, event.clientY); } function showPopup(x, y){ var popup = document.getElementById("popup"); popup.style.left = x + "px"; popup.style.top = y + "px"; popup.className = "reveal"; }
73. Create a single global (se necessário) var Controller = { handleClick: function(event){ this.showPopup(event.clientX, event.clientY); }, showPopup: function (x, y){ var popup = document.getElementById("popup"); popup.style.left = x + "px"; popup.style.top = y + "px"; popup.className = "reveal"; } };
74. Separe configurações function validate(value) { if (!value) { alert("Invalid value"); location.href = "/errors/invalid.php"; } };
203. The second parameter is the zero-based index of the first element NOT to be included in the returned slice. If omitted, the slice extends to the end of the set.