vef1-2025

Vefforritun 1 kennd haustið 2025

View the Project on GitHub vefforritun/vef1-2025

Fyrirlestur – JavaScript föll

Vefforritun 1 — TÖL107G

Ólafur Sverrir Kjartansson, osk@hi.is


People think that computer science is the art of geniuses but the actual reality is the opposite, just many people doing things that build on each other, like a wall of mini stones.

—Donald Knuth


Föll


Skilgreind föll


// skilgreint fall
function hello(name) {
  return `hello, ${name}!`;
}
hello('world'); // 'hello, world!'
hello(1); // 'hello, 1!'

Föll sem segðir


// nafnlaust fall      👇 megum nefna hér
const square = function (x) {
  return x * x;
};

console.log(typeof square); // "function"
square(3); // 9


Scope



var, let & const


let a = 1;
{ // nýtt scope fyrir block
  let a = 2;
  console.log(a); // 2
}
console.log(a); // 1

let x = 10;
if (true) {
  let y = 20;
  var z = 30;
  console.log(x + y + z);
  // 60
}
// y ekki sýnilegt hér, en z er það
console.log(x + z);
// 40

var i = 10;

function add(x) {
  // i er aðgengilegt úr global scope
  return x + i;
}
add(10);
// 20

function add(x, y) {
  // sum fer hér í global scope!
  sum = x + y;
  return sum;
}
add(1, 2); // 3
console.log(sum); // 3
let sum = 5; // 5
add(5, 6); // 11
console.log(sum); // 11


Hoisting


Hoisting dæmi

var foo = 1;
(function () {
  console.log(foo); // undefined
  var foo = 2;
  console.log(foo); // 2
})();

// túlkað sem...
var foo = 1;
(function () {
  var foo; //  undefined
  console.log(foo); // undefined
  foo = 2;
  console.log(foo); // 2
})();

let & const



Arrow functions


// eitt statement, sjálfkrafa skilað
const multiply = x => x * 2;
console.log(multiply(10)); // 20

const addition = (x, y) => {
  const sum = x + y;
  return sum;
};
console.log(addition(1, 2)); // 3


Nafnlaus föll


const hi = function (name) {
  return `hello, ${name}`;
};
hi('world');
// 'hello, world!'
hi = null; // fallið horfið!
hi('world');
// Uncaught TypeError: hi is not a function

Föll sem skilagildi

function greeter(greeting) {
  // Hér myndum við _lokun_ yfir greeting
  return name => `${greeting} ${name}`;
}
const hello = greeter('hello');
const hi = greeter('hi');
hello('world'); // "hello world"
hi('world'); // "hi world"


Færibreytur (parameters)


function alter(obj) {
  obj.bar = 1;
}
const foo = { bar: 2 };
alter(foo);
foo.bar;
// 1

Fall sem færibreyta

function foo(x, g) {
  return x + g();
}
foo(1, () => 2);
// 3

Sjálfgefin gildi í föllum

function add(a = 0, b = 0) {
  return a + b;
}

add(); // 0
add(1); // 1
add(1, 2); // 3

Variadic functions



Dæmi

// eldri leið til að skilgreina variadic föll
function avg() {
  let sum = 0;
  for (let i = 0; i < arguments.length; i++) {
    sum += arguments[i];
  }
  return sum / arguments.length;
}


Innri föll


Lokanir – closures


Lokanir - dæmi

function makeAdder(x) {
  // þetta fall hefur alltaf aðgang að x
  // því við myndum lokun
  return y => x + y;
}
const add4 = makeAdder(4);
console.log(add4(6)); // 10

function outer(a) {
  const x = 1;
  function inner(b) {
    const y = 2;
    function innerinner(c) {
      const z = 3;
      return x + y + z + c;
    }
    return innerinner(b);
  }
  return inner(a);
}
outer(10); // 16