Created by
function add(x,y){
return x+y
}
const add = function(x,y){
return x+y
};
// an anonymous function is passed as the first argument to setTimeout()
setTimeout(function() {
console.log("This message is shown after 3 seconds.");
}, 3000);
let a,b;
const foo = function () {
console.log(`Foo`);
} // Missing semicolon here
[a,b] = [4,3];
console.log(a,b);
foo();
let a,b;
const foo = function () {
console.log(`Foo`);
}; // Semicolon added here
[a,b] = [4,3];
console.log(a,b);
foo();
const foo = function add(x,y){
return x+y
};
console.log("foo(2,3):", foo(2,3)); // foo(2,3):5
console.log("add(2,3):", add(2,3)); // ReferenceError: add is not defined
const greet = new Function('userName',
'console.log(userName)');
greet('Ada')
( function(){} )();
function(){}
is an anonymous function declaration!( function(){} )
is an anonymous function expression (everything in ()
is executed as expression ).
(function(x,y){
console.log( x+y );
})(2,3);
(function(x,y){
console.log( x+y );
}(2,3));
return
operator.
function caller(f) {
// f is a "callback function"
f()
}
function foo(){
console.log(`Foo`);
}
caller(foo);
setTimeout
expects a callback function as first argument
const greeting = function(){
alert("Welcome");
}
setTimeout(greeting, 2000);
addEventListener
expects a callback function as second argument
window.addEventListener('load', function(event){
console.log('The page is fully loaded');
});
// greeting function returns a function
const greeting = function(name){
return function(){
alert("Welcome, "+ name);
}
}
setTimeout(greeting("Ada"), 2000);
const sum = function(x){
return function(y){
return x+y;
}
}
console.log( "sum:", sum(2)(3) );
function outer(){
let x = 5;
function inner(){
console.log(x);
}
return inner;
}
let f = outer();
f(); // f can access the value of local x !
var cats = [];
for (var i = 0; i < 3; i++) {
cats[i] = function(){
console.log(`Cat ${i} is ready!`);
}
}
cats[0](); //Cat 3 is ready!
cats[1](); //Cat 3 is ready!
cats[2](); //Cat 3 is ready!
i
from its outer scope, which is the loop. However, because i
is global variable, the function does not capture the value of i
, but a reference to the variable i
itself.i
variable - the global variable i
.i
is 3, that's why all of the functions logs 3i
was local or block scoped, each function created in the loop would have access to its own copy of i
at the time the function was defined. As a result, the functions would log the intended values (0, 1, 2) instead of 3, preserving the state of i
for each iteration independently.let
solutionlet
for the loop variable i
creates a unique block scope for each iteration, allowing each closure to capture and remember its own version of i
with values 0, 1,and 2 respectively.
var cats = [];
for (let i = 0; i < 3; i++) {
cats[i] = function(){
console.log(`Cat ${i} is ready!`);
}
}
cats[0](); //Cat 3 is ready!
cats[1](); //Cat 3 is ready!
cats[2](); //Cat 3 is ready!
i
as an argument,passing it to j
, creating a new scope for each iteration of the loop
var cats = [];
for (var i = 0; i < 3; i++) {
(function(j){
cats[j] = function(){
console.log(`Cat ${j} is ready!`);
}
})(i)
}
cats[0](); //Cat 0 is ready!
cats[1](); //Cat 1 is ready!
cats[2](); //Cat 2 is ready!
var cats = [];
function catMaker(i){
return function(){
console.log(`Cat ${i} is ready!`);
}
}
for (var i = 0; i < 3; i++) {
cats[i] = catMaker(i);
}
cats[0](); //Cat 0 is ready!
cats[1](); //Cat 1 is ready!
cats[2](); //Cat 2 is ready!
div.box
to each color specified in the colors array ("red", "green", "blue") in sequence, with each color change happening 1 second aparti
block scoped, declaring it with let
, but for the sake of practice imagine that you are writing a library which must works on ES5 (and let is introduced in ES6)
var colors = ["red", "green", "blue"];
var box = document.querySelector('.box');
function changeBG(color){
box.style.background = color;
}
for (var i = 0; i < colors.length; i++) {
setTimeout(function(){
console.log(`i:${i}`);
changeBG( colors[i] );
}, 1000*i)
}
function factorielIter(n) {
let res = 1;
for(let i = 1; i<=n; i++){
res *= i;
}
return res;
}
console.log(factorielIter(3));
function factorial(n){
if (n === 1) {
// f(1) = 1 => the END condition
return 1;
}else {
// f(n) = n * f(n-1)
return (n * factorial(n - 1));
}
}
console.log( factorial(3) );
function power(base, exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
console.log(power(2, 10));
These slides are based on
customised version of
framework