Vefforritun 1 kennd haustið 2024
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
return
undefinedfunction (function declaration)// skilgreint fall
function hello(name) {
return `hello, ${name}!`;
}
hello('world'); // 'hello, world!'
hello(1); // 'hello, 1!'
// nafnlaust fall 👇 megum nefna hér
const square = function (x) {
return x * x;
};
console.log(typeof square); // "function"
square(3); // 9
let, const eða varlet og const voru skilgreind var aðeins varvar er skilgreint fyrir global scope eða lexical scope fallslet og const eru skilgreind fyrir block, eru block scopedlet 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
var og þeim gefið gildi innan falls, er skilgreiningu breytu ýtt efst í scopevar 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 & constlet og const eru block scoped og losna við að þurfa hoisting
var() => {}
return// 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
const hi = function (name) {
return `hello, ${name}`;
};
hi('world');
// 'hello, world!'
hi = null; // fallið horfið!
hi('world');
// Uncaught TypeError: hi is not a function
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"
function alter(obj) {
obj.bar = 1;
}
const foo = { bar: 2 };
alter(foo);
foo.bar;
// 1
function foo(x, g) {
return x + g();
}
foo(1, () => 2);
// 3
function add(a = 0, b = 0) {
return a + b;
}
add(); // 0
add(1); // 1
add(1, 2); // 3
arguments hlut innan hvers fall sem er geymsla (ekki fylki) af færibreytum
pop, push// 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;
}
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