Skip to content

Instantly share code, notes, and snippets.

View gladchinda's full-sized avatar

Glad Chinda gladchinda

View GitHub Profile
function countryInfo(country) {
const name = country.name;
const region = country.region || 'the world';
const code2 = country.codes.cca2;
const dialcode = country.codes.dialcode;
const cities = country.cities;
return (
`
COUNTRY TIPS:
// OLD METHOD
const name = country.name;
const region = country.region || 'the world';
const code2 = country.codes.cca2;
const dialcode = country.codes.dialcode;
const cities = country.cities;
// ES6 DESTRUCTURING
const {
name,
const person = {
name: 'Glad Chinda',
birthday: 'August 15'
}
// FUNCTION WITHOUT DESTRUCTURED PARAMETERS
function aboutPerson(person = {}) {
const { name, birthday, age = 'just a few' } = person;
console.log(`My name is ${name} and I'm ${age} years old. I celebrate my birthday on ${birthday} every year.`);
// Array Destructuring
const [red, green, blue] = color;
console.log(`R: ${red}, G: ${green}, B: ${blue}`);
// R: 240, G: 80, B: 124
const [,, blue] = color;
console.log(`B: ${blue}`);
// B: 124
function fetchDatabaseRecords(fromDate, toDate) {
if (fromDate > toDate) {
// swap the dates using array destructuring
[fromDate, toDate] = [toDate, fromDate];
}
// ...execute database query
}
// The Rectangle constructor
function Rectangle(length, breadth) {
this.length = length || 10;
this.breadth = breadth || 10;
}
// An instance method
Rectangle.prototype.computeArea = function() {
return this.length * this.breadth;
}
class Rectangle {
// The class constructor
constructor(length, breadth) {
this.length = length || 10;
this.breadth = breadth || 10;
}
// An instance method
computeArea() {
return this.length * this.breadth;
// An anonymous class expression
// assigned to a variable
const Rectangle = class {
// The class constructor
constructor(length, breadth) {
this.length = length || 10;
this.breadth = breadth || 10;
}
class Rectangle {
constructor(length, breadth) {
this.length = length || 10;
this.breadth = breadth || 10;
}
computeArea() {
return this.length * this.breadth;
}
}