Skip to content

Instantly share code, notes, and snippets.

@jorenbroekema
Last active March 5, 2019 21:11
Show Gist options
  • Save jorenbroekema/db3117c7c3d75236e3fe026f31d983d6 to your computer and use it in GitHub Desktop.
Save jorenbroekema/db3117c7c3d75236e3fe026f31d983d6 to your computer and use it in GitHub Desktop.
Showcasing JS Objects, functions, template literals and arrow functions
/**
*
* 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