The document discusses several key aspects of JavaScript functions including:
1. Functions can be defined in 3 ways - with the function keyword, a function expression, or a function declaration.
2. Closures allow functions to access variables from outer scopes even after the outer function has closed. This allows functions to cache values.
3. Functions are objects that can be passed around and assigned to variables like any other object.
4. Prototypal inheritance is used in JavaScript to inherit properties and methods from one object to another using prototypes.
5. The this keyword is dynamically bound based on the invocation context of a function. Understanding how this works is important for using objects correctly.
36. var a = 5;
!
function runMe(a) {
assert( a == ___, "Check the value of a." );
!
function innerRun(){
assert( b == ___, "Check the value of b." );
assert( c == ___, "Check the value of c." );
}
!
var b = 7;
innerRun();
var c = 8;
}
!
runMe(6);
!
for ( var d = 0; d < 3; d++ ) {
setTimeout(function(){
assert( d == ___, "Check the value of d." );
}, 100);
}
37. var a = 5;
!
function runMe(a) {
assert( a == 6, "Check the value of a." );
!
function innerRun(){
assert( b == 7, "Check the value of b." );
assert( c == undefined, "Check the value of c." );
}
!
var b = 7;
innerRun();
var c = 8;
}
!
runMe(6);
!
for ( var d = 0; d < 3; d++ ) {
setTimeout(function(){
assert( d == 3, "Check the value of d." );
}, 100);
}
38. !
for ( var d = 0; d < 3; d++ ) {
setTimeout(function(){
assert( d == 3, "Check the value of d." );
}, 100);
}
39. for (var d = 0; d < 3; d++) {
(function (d) {
setTimeout(function () {
console.log(d);
}, 100);
}(d));
}
57. Functions can be defined 3
ways
Closures can be used to cache
values for later, including
execution scope.
Functions are just objects that
you can execute.
Objects inherit from each other
using prototypes.
The `this` keyword is bound to
function context
Build in object prototypes can
be extended.
No classes, just objects.
All of that works without
including jQuery at all.
59. Javascript is a great, although quirky, language.
Prototypal inheritance works similarly, but not exactly like class
inheritance.
jQuery is a great library. You should use it for DOM and AJAX code.
Reading API documentation will teach you jQuery. A good
understanding of Javascript is harder to come by.
63. var Person = Class.extend({
init: function (isDancing) {
this.dancing = isDancing;
},
dance: function () {
return this.dancing;
}
});
!
var Ninja = Person.extend({
init: function () {
this._super(false);
},
dance: function () {
// Call the inherited version of dance()
return this._super();
},
swingSword: function () {
return true;
}
});
!
var p = new Person(true);
p.dance(); // => true
!
var n = new Ninja();
n.dance(); // => false
n.swingSword(); // => true
!
// Should all be true
p instanceof Person && p instanceof Class && n instanceof Ninja && n instanceof Person && n instanceof Class