-1

I have the following sample code. I'm trying to get the this.key inside the namespace function but it's always returning undefined despite me changing the function call, or using arrow approach etc.

class Sample {
  key = '';

  constructor(key) {
    this.key = key;
  }

  myNamespace = {
    saySomething: function(message) {
      console.log('message:', message);
      console.log('key:', this.key);
    }
  }

  getTheKey() {
    console.log('key', this.key);
  }
}


let sample = new Sample('thekey');
sample.myNamespace.saySomething('message'); // shows-> key: undefined
sample.getTheKey(); // shows-> key: thekey
fes
  • 2,465
  • 11
  • 40
  • 56

2 Answers2

1

The whole point of your myNamespace property seems more than just questionable, but if you insist and still need a function that is bound to your class instance, just bind the function in the constructor, or use an arrow function which does not have its own this, but keeps this whatever it pointed to at the time of definition: (code example demonstrates both):

class Sample {
  key = '';

  constructor(key) {
    this.key = key;
    this.myNamespace.saySomething = this.myNamespace.saySomething.bind(this);
  }

  myNamespace = {
    saySomething: function(message) {
      console.log('message:', message);
      console.log('key:', this.key);
    }
  }
  
  myOtherNamespace = {
    saySomething: (message) => {
      console.log('message:', message);
      console.log('key:', this.key);
    }
  }

  getTheKey() {
    console.log('key', this.key);
  }
}

let sample = new Sample('thekey');

sample.myNamespace.saySomething('message'); // shows-> key: thekey
sample.myOtherNamespace.saySomething('other message'); // shows-> key: thekey
sample.getTheKey(); // shows-> key: thekey
connexo
  • 53,704
  • 14
  • 91
  • 128
  • why is myNamespace property questionable? I might just want to add the additional object wrapper to be more descriptive that's all. It could be something like sample.emails.saySomething or sample.instantmessaging.saySomething etc. Is there a better way? – fes May 06 '22 at 16:27
-1

"This" is pointing to two different things. "This" in your namespace function doesn't have a key value, but your constructed "sample" does - your getTheKey function accurately points to the correct "this".

To be more specific, getTheKey points to the constructed sample as "this" and the function within saySomething is pointing to saySomething as "this". The constructed sample has a value for key and saySomething does not.

You can use an arrow function instead, like so:

 myNamespace = {
    saySomething: (message) => {
      console.log('message:', message);
      console.log('key:', this.key);
    }

to target your constructed sample instead.

  • ‘the function within saySomething is pointing to saySomething as "this"’ doesn’t seem accurate. – Ry- May 05 '22 at 22:04
  • You can console log "this" within saySomething's function at the same level you're logging this.key and it points at saySomething – Taylor Morgan May 06 '22 at 04:03
  • It’ll log `myNamespace`, which contains `saySomething`. – Ry- May 06 '22 at 05:31
  • I think you're right, I was just confused by how it prints it as it doesn't directly name `myNamespace`, it prints it as `{saySomething: f}` – Taylor Morgan May 06 '22 at 21:12