Skip to content

Instantly share code, notes, and snippets.

@leo-bianchi
Created November 28, 2019 20:04
Show Gist options
  • Save leo-bianchi/ee4fec9bc8267feacd9ad5fd693ce88c to your computer and use it in GitHub Desktop.
Save leo-bianchi/ee4fec9bc8267feacd9ad5fd693ce88c to your computer and use it in GitHub Desktop.
Js file with good practices tips.
// 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();
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}`;
}
const pikachu = { name: 'Pikachu' };
const stats = { hp: 40, attack: 20, defense: 45 };
const lv10 = {...pikachu, ...stats};
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