//Advanced control flow
//TERNARY OPERATOR
var hello = 1;
console.log(hello = 1 ? "it's 1" : "something else");
// returns "it's 1
//SWITCH STATEMENT
function moveCommand(direction) {
var whatHappens;
switch(direction) {
case "forward":
whatHappens = "you encounter a monster";
break;
case "back":
whatHappens = "you arrive home";
break;
case "right":
whatHappens = "you found a river";
break;
case "right":
whatHappens = "you run into a troll";
break;
default:
whatHappens = "please enter a valid direction";
}
return whatHappens;
}
// console.log(moveCommand("forward"));
// Advanced fucntions!!!!! ==========================================
//Closure (is when child function has an access to it's parents
// function's variables even if we never execute parent function
//again )
const first = () => {
const greet = "hi";
const second = () => {
alert(greet);
}
return second;
}
const newFunc = first();
newFunc();
//Here we run first (parent) function, but it only returns us the second
//one which needs Greet from the first one. newFunc is returned Second
//and because it's a Closure it will have an access to greet var all the time
//Currying !!!! ==========================================
//Process of separating parametrs for a further use
const multiply = (a,b) => a + b; //Simple function
const curriedMultiply = (a) => (b) => a * b;
const multiplyBy5 = curriedMultiply(5); //now var multiplyBy5 holds function with an argument of 5 inside it at all times
//now we can do this:
multiplyBy5(5); //Which will give us 25 as an answer
//COMPOSE !!!!!!!!!!!!!!!!!!!!!! ==========================================
const compose = (f,g) => (a) => f(g(a));
//variable = (function 1, function 2) => (parametr) => function 1(function 2(parametr))
const sum = (num) => num + 1;
compose(sum,sum)(5) //Will give us 7
//Maths behind :
//compose = (sum,sum) => (5) => ((5 + 1) + 1);
//ES6 Destructuring ==========================================
const obj = {
player: "bobby",
experience: 100,
wizardLevel: false
}
// instead of:
const player = obj.player;
//Do this:
const { player, experience } = obj;
// Creating an object ==========================================
//we can also do weird key names now in objects:
const name = "Jon Snow";
const obj = {
[name]: "Hello",
["ray" + "smith"]: "hihi",
[1+2]: "hi"
}
// ==========================================
const a = "Simon";
const b = true;
const c = {};
//Now if we want both property and a var names to be the same we can do this
const obj = {
a, b, c
}
//TEMPLATE STRINGS ==========================================
const friend = "Slavik";
const state = 50
const string = `Hello my ${friend} , how are you my ${state - 25}`;
//DEFAULT ARGUMENTS ==========================================
const random = (name='Duk', age='20', pet='dog') => {
console.log(`hello my ${name} of age ${age} how you ${pet}`)
}
//SYMBOL ==========================================
//These are unique identifiers for object properties most of the time
const sym1 = Symbol();
const sym2 = Symbol('Haha');
const sym3 = Symbol('fooo');