console.log('Buya!')
//First Class Citizens
const age = 100;
const cool = true;
//Anon function
// function (firstName) {
// return `Dr. ${firstName}`;
// };
//Cannot run before initializing the function as a const.
// doctorize('Wes');
// console.log(doctorize('Wes')); <-- This works.
//Function Expression
const doctorize = function(firstName) { // <-- This functions are not hoisted.
doesntExist();
return `Dr. ${firstName}`;
};
//Arrow functions <-- they are annonymous functions(always in a variable)
// function inchToCM(inches) {
// const cm = inches * 2.54;
// return cm;
// }
//Function as it was:
// function inchToCM(inches) {
// const cm = inches * 2.54;
// return cm;
// }
// Step 1:
// const inchToCM = function(inches) {
// const cm = inches * 2.54;
// return cm;
// }
// Step 2:
// const inchToCM = function inchToCM(inches) {
// return inches * 2.54;
// }
// Step 3:
// const inchToCM = (inches) => {
// return inches * 2.54;
// }
// Step 4:
// const inchToCM = (inches) => inches * 2.54;
// Step 5:
const inchToCM = inches => inches * 2.54;
//Steps to create an arrow function:
// 1. Make it a function in a variable. Name the function at variable level.
// 2. Remove the const for the return and just return the resolution of the expression
// 3. Remove function text and place fat-arrow on the right side of parenthesis.
// 4. Remove explicit return and the brackets.
// 5. If there's only one argument remove parenthesis
// function add(a, b = 3) {
// const total = a + b;
// return total;
// }
const add = (a, b = 3) => a + b;
// function makeABaby(first, last) {
// const baby = {
// name: `${first} ${last}`,
// age: 0
// }
// return baby;
// }
// const makeABaby = (first, last) => {
// const baby = {
// name: `${first} ${last}`,
// age: 0
// }
// return baby;
// }
const makeABaby = (first, last) => ({ name: `${first} ${last}`, age: 0 });
// const percentageChange = (num1, num2) => `${ parseInt( ((num2 - num1) / num1) * 100 ) }%`;
const percentageChange = (num1, num2) => `${ ( ((num2 - num1) / num1) * 100 ).toFixed(2) }%`;
// IIFE
// Immediately Invoked Function Expression
// Wrap a function in parenthesis and then add (); at the end.
// (function() {
// console.log('Running the annon function')
// return 'You are cool';
// })();
(function(age) {
console.log('Running the annon function');
console.log( `You are cool and age ${age}`)
return `You are cool and age ${age}`;
})(10);
// Method
// A function that lives inside of an object.
// console is an object
// log is the function
const wes = {
name: 'THIS NAME: Wes Bos',
sayHi: function() {
console.log(this.name);
console.log('Hey Wes');
return 'Hey Wes';
},
yellHi() {
console.log('HEY WESSSS');
},
whisperHi: () => {
console.log('hi wes im a mouse')
}
}
//Callback Functions
const button = document.querySelector('.clickMe');
function handleClick() {
console.log('Great clicking!')
}
button.addEventListener('click', handleClick);
button.addEventListener('click', function() {
console.log('damn!');
});
setTimeout(wes.yellHi, 1000);