javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

fix(heap): `this` is `undefined`

Open ojj1123 opened this issue 1 year ago • 1 comments

What's the problem?

  • MaxHeapAdhoc and MinHeapAdhoc constructor 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: 스크린샷 2024-04-13 01 19 15

  • The problem is that if passing this.add method literally, this used by add method is undefined.

  • This issue is confusing for the developers. And your intend is that MaxHeapAdhoc and MinHeapAdhoc is 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));

ojj1123 avatar Apr 12 '24 17:04 ojj1123

@trekhleb I was wondering if you could review this PR! I think it is important since your intend could be broken up.

ojj1123 avatar Apr 17 '24 06:04 ojj1123