class Mammal {
constructor(dob) {
this.dob = dob;
}
get age() {
let today = new Date();
return today.getFullYear() - dob.getFullYear();
}
}
class Person extends Mammal {
constructor(firstName, lastName, dob) {
super(dob);
this.firstName = firstName;
this.lastName = lastName;
}
get fullName() {
return this.firstName + ' ' + this.lastName;
}
greet() {
console.log("Hello, my names is " + this.fullName + ", I am " + this.age + " years old").
}
}
var me = new Person("Jason", "Perry", new Date(1980, 11, 12));
me.greet(); // --> "Hello, my name is Jason Perry, and I am 34 years old"
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
Closures & Scope
Slide 13
Slide 13 text
“…an inner function always has access to
the vars and parameters of its outer
function, even after the outer function has
returned.”
- Douglas Crockford
Slide 14
Slide 14 text
function box() {
var key = "setec astronomy";
return {
read: function() {
return this.decrypt(key);
},
decrypt: function(message) {
// ...
}
}
}
var secret = box();
secret.read() // --> "too many secrets"
Slide 15
Slide 15 text
(function() {
// ...
})();
Slide 16
Slide 16 text
Higher-order
functions
Slide 17
Slide 17 text
var square = function (n) {
return n * n;
}
var print = function (m) {
console.log(m);
}
[1, 2, 3, 4, 5].map(square); // --> [2, 4, 9, 16, 25]
[1, 2, 3, 4, 5].map(square).forEach(print); // logs 2, 4, 9, 16, 25