23. const
function process() {
const factor = 10;
let result = 5;
result *= 3;
// thousands lines of code...
return result * factor;
}
Most variables don't change over time.
Using const helps spot unwanted side effects.
24. constconst = better let
function process() {
const factor = 10;
let result = 5;
result *= 3;
// thousands lines of code...
return result * factor;
}
Most variables don't change over time.
Using const helps spot unwanted side effects.
25. “I am a full
const nazi
nowadays”
John Carmack: "I am a full const nazi nowadays, and I chide any programmer that doesn’t
const every variable and parameter that can be."
http://www.phoronix.com/scan.php?page=news_item&px=MTI3NDQ
26. Use let and const today
defs.js
http://blog.lassus.se/2013/05/defsjs.html
34. let nodeArray = [...document.querySelectorAll("p")];
var nodeList = document.querySelectorAll("p");
var nodeArray =[].slice.apply(nodeList);
ES6
ES5
36. function tax(rate, ...values) {
let taxedValues = [];
for (let value of values) {
taxedValues.push(rate * value);
}
return taxedValues;
}
function tax() {
var args = Array.prototype.slice.call(arguments);
var rate = args[0];
var values = args.splice(1);
var taxedValues = [];
for (var i = 0; i < values.length; i++) {
var value = values[i];
taxedValues.push(rate * value);
};
return taxedValues;
}
ES5
ES6
37. function tax(rate, ...values) {
let taxedValues = [];
for (let value of values) {
taxedValues.push(rate * value);
}
return taxedValues;
}
function tax() {
var args = Array.prototype.slice.call(arguments);
var rate = args[0];
var values = args.splice(1);
var taxedValues = [];
for (var i = 0; i < values.length; i++) {
var value = values[i];
taxedValues.push(rate * value);
};
return taxedValues;
}
ES5
ES6
38. Iterators
let elements = ["one", "two", "three"];
elements.customProperty = "Won't show up";
for (let element of elements) {
console.log(element);
}
// "one" "two" "three"
39. Object.prototype.dontDoIt = "bwahaha";
var obj = {name: "Chris", surname: "Szafranek"};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key, obj[key]);
}
}
Object.prototype.dontDoIt = "bwahaha";
var obj = {name: "Chris", surname: "Szafranek"};
import items from @"iter";
for (let [key, value] of items(obj) {
console.log(key, value);
}
ES6
ES5