January 2018
Intermediate to advanced
332 pages
7h 36m
English
Since the code for an Angular application is now in TypeScript, we can further optimize the stack that we created. Using TypeScript makes the code more readable thanks to the private variables that can be created in a TypeScript class.
So, our TypeScript-optimized code would look something like the following:
export class Stack { private wmkey = {}; private items = new WeakMap(); constructor() { this.items.set(this.wmkey, []); } push(element) { let stack = this.items.get(this.wmkey); stack.push(element); } pop() { let stack = this.items.get(this.wmkey); return stack.pop(); } peek() { let stack = this.items.get(this.wmkey); return stack[stack.length - 1]; } clear() { this.items.set(this.wmkey, []); } size() { return this
Read now
Unlock full access