ES6 Tricks Flashcards

Learn the tricks of the trade

1
Q

How do you add a date to an object?

A

Assuming your obj has a datestamp method, then:

function addDate(myObj) {
   const months = [
      'January', 'February', ..., 'December']; // fill in the rest
     const d = new Date(myObj.dateStamp);
     if  (isNaN(d)) { throw new Error('Wrong timestamp');}
     const dateString = `${d.getDate()} ${months[d.getMonth()]} ${d.getFullYear()}`;
    return {dateString, ...myObj};
}
test it:
let myObj = {a:1, b:2};
myObj.dateStamp = Date.now(); // 1563448236079
addDate(myObj); // {dateString: "18 July 2019", a:1,b:2};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do you use instead of strings in an app error control and why?

A

You will have to use Error types rather than error strings - strings are code smells, if you want to internationalize the code you’ll get in trouble.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly