1

How can I access class members during the creation of an anonymous object?

class foo {
   private working = 'hello';
   private notWorking = 'world';

   public method() {
      new DoSomethingClass(working, {
         ...,
         this.notWorking //this throws an error as this has a different scope now
      });
   }
}
7
  • 1
    Did you try the approach to store this in another variable before the scope change? E.g. const self = this; right before you do new DoSomethingClass and then use self within the new scope. It may still not be possible to access a private property that way though. Commented Sep 9, 2019 at 12:19
  • 1
    Your class syntax is also wrong. The : are not valid there. Commented Sep 9, 2019 at 12:21
  • 1
    And it also may help to describe what you are trying to achieve. What do you want to inject in the DoSomethingClass? I am confused as to why you want to add the logic inside the foo class and not the DoSomethingClass. Commented Sep 9, 2019 at 12:23
  • I fixed the ":", this was just a typo. TheDoSomethingClass is from a lib and takes a parameter object. I want to pass the arguments to it that i have stored in a class. Commented Sep 9, 2019 at 12:24
  • 1
    I think you are missing the key of your config object. {theKeyTheLibWants: this.notWorking} Commented Sep 9, 2019 at 12:28

2 Answers 2

4

When passing a config object, make sure it uses the right key:

  new DoSomethingClass(working, {
     theRequiredKey: this.notWorking
  });

Check your library for the key names.

You can still use a shortcut for that you use the same variable name as the key is called, e.g.:

  const theRequiredKey = this.notWorking;
  new DoSomethingClass(working, {
     theRequiredKey, // will create an object with key "theRequiredKey" and its value
  });

This will work as wanted.


Also check if your library has typescript typings available, e.g. via

npm install @types/{library-name}

or create your own typings.

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

Comments

1

At runtime there is no way to guarantee that this will have the right scope. A function's scope (proper term is "context") can be customized at call time with bind/call/apply, and if separated from it's object, it loses its original context:

var fn = instance.method;
fn(); // `this` will not be `instance` anymore

Arrow functions allow you to avoid this issue.

public method = () => { ... }

There are other ways like stashing var _this = this, but that's error prone.

Note: arrow class methods are different than regular class methods. Since they need to access instance variables, the typescript-generated function will not be on the prototype, but rather in the class constructor. Each instance will have a separate function instance, instead of all pointing to the same function instance. This might affect performance in hot code paths.

Comments

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.