3. Recap
function counter(count) {
if (count === undefined) {
count = 1;
}
console.log(count);
if (count < 10) {
counter(count + 1);
}
}
counter();
Wednesday, 23 October 13
4. Recap
function fib(n) {
if (n < 2) {
return n;
}
return fib(n - 1) - fib(n - 2);
}
Wednesday, 23 October 13
5. Recap
function greeter(greeting) {
var count = 0
;
function greet(name) {
count++;
}
}
console.log(greeting + ' ' + name + '! (#' + count + ')');
return greet;
hi = greeter('Hi');
hi('Bob'); // Hi Bob (#1)
hi('Fred'); // Hi Fred (#2)
count // undefined
Wednesday, 23 October 13
6. Recap
function Animal(name, sound) {
this.name = name;
this.sound = sound;
}
dog = new Animal('fido', 'woof!');
cat = new Animal('puss', 'meow!');
dog.name // fido
cat.name // puss
Wednesday, 23 October 13
7. Recap
function Animal(name, sound) {
this.name = name;
this.sound = sound;
}
Animal.prototype.greet = function() {
console.log(this.sound);
}
dog = new Animal('fido', 'woof!');
cat = new Animal('puss', 'meow!');
dog.greet();
cat.greet();
Wednesday, 23 October 13
8. Recap
function Dog(name) {
this.name = name;
this.sound = 'woof!';
}
Dog.prototype = new Animal(null, null);
dog = new Dog('fido');
Wednesday, 23 October 13
11. Refactoring
var el;
el = document.getElementById('foo');
el.style.color = 'red';
el = document.getElementById('bar');
el.style.color = 'red';
el = document.getElementById('baz');
el.style.color = 'red';
Wednesday, 23 October 13
12. Refactoring
function makeRed(id) {
var el = document.getElementById('foo');
el.style.color = 'red';
}
makeRed('foo');
makeRed('bar');
makeRed('baz');
Wednesday, 23 October 13