Search⌘ K

Step 3: Account Module

Explore how to build an Account module using ES6 class syntax and configure it with Webpack. Understand managing transactions through importing UnderscoreJS functions, and learn practical coding steps to create, sort, and manage account transactions effectively.

We'll cover the following...

To process transactions, we need an Account class. We will use the class syntax of ES6 to write the code. A short explanation will follow the example. Before reading the explanation, try to figure out what the code does.

Javascript (babel-node)
import { sortBy, first } from 'underscore';
class Account {
constructor() {
this.transactions = [];
}
getTopTransactions() {
var getSortKey = transaction =>
-Math.abs( transaction.amount );
var sortedTransactions = sortBy(
this.transactions,
getSortKey
);
return first( sortedTransactions, 3 );
}
deposit( amount, date ) {
this.transactions.push({
amount : amount,
date : date
});
}
withdraw( amount, date ) {
this.transactions.push({
amount : -amount,
date : date
});
}
};
export default Account;

Save the above code in src/Account.js. We will ...