Created
November 28, 2019 20:04
-
-
Save leo-bianchi/ee4fec9bc8267feacd9ad5fd693ce88c to your computer and use it in GitHub Desktop.
Js file with good practices tips.
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
// DEBUG | |
// Printing objects on console | |
const foo = {name: 'tom', age: 30}; | |
const bar = {name: 'harry', age: 50}; | |
const baz = {name: 'john', age: 19}; | |
console.log({ foo, bar, baz }) // To show objects names on console. | |
// Styling with css | |
console.logo('%c My friends', 'color: orange; font-weight: bold'); | |
// Console table | |
console.table([foo, bar, baz]); | |
// Stack Trace Logs | |
const deleteMe = () => console.trace('function execution'); | |
deleteMe(); | |
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
const turtle = { | |
name: 'Bob', | |
legs: 4, | |
shell: true, | |
type: 'amphibious', | |
meal: 10, | |
diet: 'berries' | |
}; | |
function feed({ name, meal, diet }) { | |
return `Feed ${name} ${meal} kilos of ${diet}`; | |
} | |
// OR | |
function feed(animal) { | |
const { name, meal, diet } = animal | |
return `Feed ${name} ${meal} kilos of ${diet}`; | |
} |
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
const pikachu = { name: 'Pikachu' }; | |
const stats = { hp: 40, attack: 20, defense: 45 }; | |
const lv10 = {...pikachu, ...stats}; |
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
const horse = { | |
name: 'Topher', | |
size: 'large', | |
skills: ['jousting', 'racing'], | |
age: 7 | |
}; | |
// Bad code | |
let bio = horse.name + ' is a ' + horse.size | |
// Good Code | |
const { name, size, skills} = horse; | |
bio = `${name} is a ${size}`; | |
// Functional way | |
function horseAge(str, age) { | |
const ageStr = age > 5 ? 'old' : 'young'; | |
return `${str[0]}${ageStr} at ${age} years`; | |
} | |
const bio2 = horseAge`This horse is ${horse.age}`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment