The document provides sample code examples for key Node.js concepts including prototype-based object-oriented programming, asynchronous programming with callbacks, promises, and async/await, automated testing with Mocha and Chai, and using TypeScript with Node.js. The examples cover topics such as object prototypes, classes, timers, promises, generator functions, generics, and writing automated tests. Useful links are also provided for further learning Node.js, asynchronous programming, testing, and TypeScript.
3. 1. Prototype-based OOP
• It is like working with templates and copying those templates
• Object prototypes are dynamic; they can be changed at run-time
• Objects based on those prototypes are also dynamic
• Prototypes can copy behaviour of other prototypes (Inheritance)
• Multiple prototypes can implement same behaviour (Polymorphism)
4. 1. Sample Code (a)
// 1.a. generic object
const person0 = { name: 'Adam', age: 101, friends: [] };
person0.follow = friend => { this.friends.push(friend); };
const person1 = Object.create(person0);
// 1.b. we can also put it inside a 'factory' function and return person object
const makePerson = (name = '', age = 0, friends = []) => { /* create person0 */ return person0; };
// 2. function definition
function Person(name = '', age = 0, friends = []) {
this.name = name;
this.age = age;
this.friends = friends;
this.follow = friend => { this.friends.push(friend); };
}
const person2 = new Person('Becky', 102);
// 3. class definition
class PersonModel {
constructor(name = '', age = 0, friends = []) {
this.name = name;
this.age = age;
this.friends = friends;
}
follow(friend) { this.friends.push(friend); }
}
PersonModel.prototype.talk = sentence => { console.log(sentence); /* TODO use TTS engine */ }
const person3 = new PersonModel('Conor', 103);
Ref: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS
5. 2. Asynchronous Programming
• Callback functions: Fire and forget; we will call you back!
• Functions as arguments to other functions
• Timers: setTimeout(), setInterval()
• Promises: special object that “mimic” threads
• Special methods: then(), catch()
• Callback hell:
• Async/Await heaven
6. 2. Sample Code (a)
// Timers
function showTime() { console.log(new Date()); }
const id = setInterval(showTime, 1000); // run every second
function stopShowingTime() { clearInterval(id); console.log('stopped'); process.exit(0); }
setTimeout(stopShowingTime, 10 * 1000); // run after ten seconds once
console.log('started');
// Promises
function promiseHandler(resolve, reject) {
const r = Math.random();
0.9 <= r // ternary expression used as statement
? resolve({ success: 'we got: ' + r }) // captured by then() callback
: reject({ error: 'sorry, we got: ' + r }); // captured by catch() callback
}
const randomPromise = new Promise(promiseHandler);
const startRandomPromise = () => {
randomPromise.then(console.info).catch(console.error);
}
startRandomPromise();
8. 3. Automated Tests
• “Mocha is a feature-rich JavaScript test framework running on
Node.js and in the browser”
• Define test suites/cases by using simple function calls and callback functions
• Use async/await within callback functions, if preferred,
• “Chai is a BDD / TDD assertion library for node and the browser”
• Verify the expectations using chainable interfaces: expect, should, assert
13. The End
Thank you
Useful links:
https://nodejs.dev/learn
https://pptr.dev/
https://mochajs.org/
https://www.chaijs.com/
https://istanbul.js.org/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator
https://www.typescriptlang.org/