0

I have a config object with a token. Inside, there is a nested headers object that needs to reference the token. I am unable to use this, as shown below (comes up as undefined), and config.token doesn't work either.

var config = {
  token: 'abc123',
  headers: {
    'Authorization': 'bearer ' + this.token
  }
}

I need to be able to reassign config.token and have config.headers.Authorization update automatically with the new value.

0

1 Answer 1

5

You could do it with get operator :

var config = {
  token: 'abc123',
  get headers () {
    return {
      'Authorization': 'bearer ' + this.token
    }
  }
}

console.log(config);

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I didn't know about the get operator. Thank you, this works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.