How to access variable key and use it as a variable in another function?

Related searches

I'm sorry about the title I'm not sure what is the name of the issue I'm trying to solve.

Assume I have the following class with a function

{
 printInfo(age){
 console.log(name)
 console.log(age)
 }
}

From another page I want to call the class like this: someClass.Mike.printInfo(21), and the function would print Mike and 21. Of course, the name Mike is variable so it can be anything.

Is there a way to accomplish that? Maybe something special in the constructor of the class? some JSON object keys manipulations?

Thank you.

Assuming you have stored the Names somewhere for example:

const names = ['Mike', 'Dave'];

you could access the function, if it is indeed stored in the regarding objects with dynamic properties like this:

names.forEach(name => {
   someClass[name].printInfo(21);
})

If that is not what you need you should try to write your question a little bit more detailed.

The statement from another page is also a little bit confusing, if the class is not defined on the page than you cannot access it.

How to Access Function variables in Another Function, Return them from your first function and accept them in your second function. Example - def xxx(): a=10 b=15 c=20 return a,b def yyy(): a,b� You will need to call the ‘initial’ function somewhere in your code or in the ‘calcular’ function to put ‘nproduto’ in the ‘calcular’ function’s workspace. Please do not use global variables to share data between workspaces !

You'll need to pass any variables being used. You can do so by separating them:

    printInfo(name, age)

To use it, you'd call the method like so:

    SomeClass.printInfo("Mike", 21);

How to call a variable from another function in Python, If u r asking that how to call a variable of 1 function into another function , then possible ways are - 1. This is how I understand your question: How do I access a variable defined in a function in Python? If we did not use the global key. It's a local variable, which means it only exists when the function is actually being executed. At the end of the function it goes out of scope and it no longer available. In the real world, it's actually worse: the variable itself is created on the stack, and when the function execution ends the stack space is released so the variable value is

Create an instance for each person and pass the name in the constructor then call printInfo() from an instance to get the name and age :

class SomeClass {
 
   constructor(name, age) {
     this.name = name;
     this.age = age;
   }
 
   printInfo(age){
     console.log(this.name)
     console.log(age || this.age)
   }
}

const mike = new SomeClass('Mike');
mike.printInfo(21);

const jean = new SomeClass('Jean', 17);
jean.printInfo();

Programming FAQ — Python 3.8.5 documentation, What are the “best practices” for using import in a module? In the example above you can access the outer scope variable by declaring it global: In Python , variables that are only referenced inside a function are implicitly global. reference to one dict for all calls compute something mydict[key] = value return mydict. If another function needs to use a variable you pass it to the function as an argument. Also global variables are not inherently nasty and evil. As long as they are used properly there is no problem with them.

Variable variables - Manual, In the above example, hello, can be used as the name of a variable by using They are most useful when accessing values within a property that contains variables cannot be used with PHP's Superglobal arrays within functions or To use the function, when you want to reference an array, send it in the form 'array: key'� Exercise 1: Create a new project using the source code from Local Variables in a Function. Build and run. Here’s the outpute: In the main function, a=365 In the vegas function, a=-10 In the main function, a=365. Even though the same variable name is used in both functions, it holds a different value.

Using variables, A variable is a symbolic representation of data that allows you to access a value without Variables in Postman are key-value pairs. for example if you use one set of config details for your production server and another for testing. variable in your scripts using the object representing the scope level and the .get method:. If a variable is going to be used throughout the program it is important that you declare the variable in global scope. But, because JavaScript allows re-declaration of same variables, it can easily become a problem if a function is using a variable with same name as global variable in its scope.

As you can see, the { thetop : 10 } declaration doesn't make use of the variable thetop. Instead it creates an object with a key named thetop. If you want the key to be the value of the variable thetop, then you will have to use square brackets around thetop:

Comments
  • Your question is very unclear; you don't "call" classes, for one, and all your JS is on every page in which you've included the script tags . Do you mean you want to access the class method printInfo() from some other context? And what you're asking w/r to the name is completely unclear. Do you mean you someClass[var_containing_property_name].printInfo()? Why wouldn't printInfo() just be a method of someClass so you can access it whenever you want, and provide the name along with the age as arguments, ie someClass.printInfo(Mike, 21)?
  • What you are asking for is impossible. At the time when you use the value of a variable or a property, you cannot know what the variable or a property name was. So if you call myFunction() or myObject.myMethod() you don't actually have those names available. At most you can have named functions, e.g., function someFunction() {} but if you then do someNewName = someFunction you are still unable to figure out if you've called someFunction() or someNewName() at execution time.
  • Thank you for the response. Yes I'm trying to do someClass[var_containing_property_name].printInfo() and use that var_containing_property_name inside the printInfo(). I was trying to see if there is a way to do that. I did not want to say it is impossible right away, I wanted to see if there is something I'm missing. Thank you.
  • Thank you, but that's not what I'm looking for. That is just an array and you are calling the method on the array items.
  • I am not calling the methods on the array items I am calling the methods on the objects someClass which are named as the array items
  • Thank you. But I was trying to find a solution where name is not part of the variables that go inside that function, instead it is in someClass."variable".printInfo(age).