0

Hi everyone I've just started javascript recently and I am trying to learn about classes and objects but I ran into a small bump that I could not get through so here is my code

class bubbleSorter{
  constructor(/*params*/){
  //some fields
  }
  #bubbleSorter = () =>{
  //some code
  }

  getBubbleSorter = () =>{
    this.#bubbleSorter();
  }
}

let obj = new bubbleSorter(/*params*/);

console.log(obj.getBubbleSorter());

so this is an example of my code. whenever I try to log the getter method it does not quite work but when I call the bubbleSorter method instead --Assuming I remove the # symbol-- it magically works I do not know why is that happening I tried making bubbleSorter public then calling it inside the getBubbleSorter it still does not work

  • Don't use arrow functions as class methods. – Barmar Jul 01 '21 at 19:58
  • @Barmar how come? – evolutionxbox Jul 01 '21 at 19:59
  • Because they don't get `this` passed to them, they retain the original `this` from when they were defined. – Barmar Jul 01 '21 at 20:00
  • `get bubbleSorter() { /*somecode*/ }` – Keith Jul 01 '21 at 20:01
  • "*whenever I try to log the getter method it does not quite work*" what do you mean? Your `getBubbleSorter()` method doesn't return anything, so it always implicitly produces `undefined`. That's common behaviour for all functions. If your `#bubbleSorter()` is supposed to return a value, then you need `return this.#bubbleSorter();` inside `getBubbleSorter()`. If it's not that, you need to explain what "doesn't work" means here. – VLAZ Jul 01 '21 at 20:06
  • @Barmar It Still does not work even though i changed it to normal functions – An aspiring Scientist Jul 01 '21 at 20:11
  • @Barmar "*they retain the original this from when they were defined*" which would be the object creation, so `this` will point to the created instance. The values should be correct in this case. Unless the constructor swaps out `this` for some other instance. https://jsbin.com/qonudesalu/1/edit?js,console – VLAZ Jul 01 '21 at 20:13
  • 1
    Please post a [mcve] and explain what the code is supposed to do. – Barmar Jul 01 '21 at 20:13
  • @VLAZ Oh, right. The linked question is about methods in object, not methods in classes. – Barmar Jul 01 '21 at 20:14

2 Answers2

0

getBubbleSorter calls this.#bubbleSorter but doesn't return the value, causing the function to return undefined.

UnsignedByte
  • 849
  • 10
  • 29
0

it looks like you aren't returning anything in your example. Specifically for

getBubbleSorter = () =>{
  this.#bubbleSorter();
}

If you change it to

getBubbleSorter = () =>{
  return this.#bubbleSorter();
}

I think you'll start getting what you expect. What your example currently is doing at the end is the equivalent of

console.log(() => {}());

right now it's executing a function that has no return value, so it logs out undefined. Once you add that return above now you will be logging the result of your private getter.

Alexander Kahoun
  • 2,458
  • 24
  • 36