javascript-algorithms
javascript-algorithms copied to clipboard
fix(heap): `this` is `undefined`
What's the problem?
MaxHeapAdhocandMinHeapAdhocconstructor can get an array for filling the heap initially.- And then the constructors execute this:
this.heap.forEach(this.add);
-
But It did occur this error:
-
The problem is that if passing
this.addmethod literally,thisused byaddmethod isundefined. -
This issue is confusing for the developers. And your intend is that
MaxHeapAdhocandMinHeapAdhocis used by developers easily
https://github.com/trekhleb/javascript-algorithms/pull/1117
This PR contains several minimalistic (by their functionalities and implementation) data structures like MinHeap, MaxHeap, and DisjointSet that don't have external dependencies and that are easy to copy-paste and use during the coding interview if allowed by the interviewer.
How to fix
- Just use arrow function
this.heap.forEach((value) => this.add(value));
@trekhleb I was wondering if you could review this PR! I think it is important since your intend could be broken up.