Last active
March 5, 2019 21:11
-
-
Save jorenbroekema/db3117c7c3d75236e3fe026f31d983d6 to your computer and use it in GitHub Desktop.
Showcasing JS Objects, functions, template literals and arrow functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 1) OBJECTS YAY!!! | |
* | |
*/ | |
const animals = { | |
'turtle': 'π’', | |
'unicorn': 'π¦', | |
} | |
const animals = { | |
turtle: { | |
'emoji': 'π’', | |
'age': 300, | |
'name': 'Bob', | |
}, | |
unicorn: { | |
emoji: 'π¦', | |
age: Infinity, | |
name: 'Joren', | |
} | |
} | |
const animals = { | |
'π’': { | |
'age': 300, | |
'name': 'Bob', | |
}, | |
'π¦': { | |
age: Infinity, | |
name: 'Joren', | |
} | |
} | |
console.log(animals['π’']); | |
console.log(animals['π’'].age); | |
/** | |
* | |
* 2) FUNCTIONS YAY!!! | |
* | |
*/ | |
function doSomething() { | |
console.log('I am doing something :)'); | |
} | |
doSomething(); | |
function returnSomeString() { | |
console.log('start of the function'); | |
return 'I am returning this string'; | |
console.log('this part does not get executed'); | |
} | |
let someString = returnSomeString(); | |
function sumIsMoreThan(numberA, numberB, minimumSum) { | |
const sum = numberA + numberB; | |
if (sum > minimumSum) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
console.log(sumIsMoreThan(5, 23, 15)); | |
console.log(sumIsMoreThan(5, 7, 20)) | |
/** | |
* | |
* 3) BACK TO OBJECTS YAY!!! | |
* | |
*/ | |
const animals = { | |
turtle: { | |
emoji: 'π’', | |
age: 300, | |
name: 'Bob', | |
giveFood: function () { | |
console.log(this.emoji + ': Thank you for feeding me that π!!!'); | |
}, | |
}, | |
unicorn: { | |
emoji: 'π¦', | |
age: Infinity, | |
name: 'Joren', | |
poop: function (amount = 2) { | |
poops = ''; | |
for (let i = 0; i < amount; i++) { | |
poops += 'π'; | |
} | |
console.log('π¦' + poops) | |
}, | |
getAge: function () { | |
return this.age; | |
}, | |
friends: ['π’', 'π', 'πΉ'], | |
// getMyself: function() => { return this } // show this to demonstrate arrow vs normal function "this" keyword | |
} | |
} | |
animals.unicorn.poop(5); | |
animals.turtle.giveFood(); | |
let unicornAge = animals.unicorn.getAge(); | |
console.log(`${animals.unicorn.emoji}: My age is ${unicornAge - 1}, whoopwhoop!!`); | |
console.log(animals.unicorn.friends); | |
/** | |
* Arrow functions :O omg | |
*/ | |
let sumIsMoreThan = function(numberA, numberB, minimumSum) { | |
const sum = numberA + numberB; | |
if (sum > minimumSum) { | |
return true; | |
} | |
return false; | |
} | |
console.log(sumIsMoreThan(15, 5, 30)) | |
// Change it to this: | |
sumIsMoreThan = (numberA, numberB, minimumSum) => { | |
const sum = numberA + numberB; | |
if (sum > minimumSum) { | |
return true; | |
} | |
return false; | |
} | |
let normalFunction = function () { | |
return true; | |
}; | |
let arrowFunction = () => true; | |
console.log(normalFunction(), arrowFunction()); | |
// Oh wow they are not the same exactly? | |
console.log(normalFunction == arrowFunction); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment