0

I would like to know how I can write a method in such a way that I can later call a variable stored in it.

Of course, after I export and import into another Javascript file, I want to call the variable from the method, but I don't know exactly the syntax and if I have to use 'this'

I also don't know what to return to have all the declared variables available at any time

I want something like

class Security {
  chooseEntrance(FrontEntrance, BackEntrance) {
    FrontEntrance = //some code//
    BackEntrance = //some code//
  }
}

export default Security

And from another file, after import, call something like Security.chooseEntrance(FrontEntrance)

I hope I've set out all the data I need

4
  • Do you meant to access local variable defined in the method of a class from another class or function ? Commented Jan 19, 2022 at 15:59
  • 2
    You're probably thinking about this wrong. You may want a getter method to retrieve the value, or you may want to expand the scope of the variable to the class or window object (global). Be sure you're not asking an xy question. Commented Jan 19, 2022 at 15:59
  • Variables inside a function are local to the function and only exist when the function is executed. Commented Jan 19, 2022 at 16:01
  • Whatever you do, format your code properly in the future, for your benefit and ours. Commented Jan 19, 2022 at 16:03

2 Answers 2

1

You can't do that. By typing Security.chooseEntrance(FrontEntrance) you ask to execute the function and not get the variable in the function. We need more context to answer you propely but an answer can be either to store the variable somewhere in the script wich call the function or to create a field in the class Security and retrieve it later with a getter like this:

class Security{
    frontEntrance

    getFrontEntrance(){
        return frontEntrance
    }

    chooseEntrance(FrontEntrance,BackEntrance){
      FrontEntrance=//some code//
      BackEntrance=//some code//
    }

  }
export default Security
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. Something dubious happened to me and I would like to ask you why. In the sense that from everything you wrote to me in the code, I only managed to get the sequence class Security{ chooseEntrance(FrontEntrance,BackEntrance){ FrontEntrance=//some code// BackEntrance=//some code// } } export default Security and then import it in another file and call with security.chooseEntrance('FrontEntrance') and it works. Why without getter and without first declaration "frontEntrance" it's works?
0
class Security {
  FrontEntrance;
  BackEntrance;
  chooseEntrance(FrontEntrance, BackEntrance) {
    this.FrontEntrance = FrontEntrance;
    this.BackEntrance = BackEntrance;
  }
}

export default Security;

from some other class you access

const security = new Security();
security.chooseEntrance('', '');


security.FrontEntrance;
security.BackEntrance;

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.