Get the name of the argument from inside the function? [duplicate] - javascript

This question already has answers here:
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Determine original name of variable after its passed to a function
(9 answers)
Closed 3 years ago.
Is it possible to get the name of the variable passed in as an argument from inside of the function, if a variable has been passed in?
function foo(bar){
// how to get the string `obj`?
}
const obj = {a:1};
foo(obj)

Related

How to name an anonymous function inside a javascript function based on passed parameters? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 9 months ago.
I want to give the anonymous function's name that is inside my function based on the parameters passed. For example, if the passed parameter is "nature" then I want to create an anonymous function with that name.
Example code:
function hello(nature) {
window.nature /* I want this word "nature" to be taken from the parameter passed by the function */ = function () {
console.log('succes');
}
}
You can do it by passing a string as parameter.
Here how to assign a custom name and a custom function, if needed.
function hello(name, func) {
window[name] = func;
}
hello('azerty', () => console.log('hello'));
window.azerty() // output 'hello'
Here is an exemple where variable "nature" has value "fn" and parameter passed to "fn" is logged to the console.
function hello(method) {
window[method] = function (param) {
console.log(param);
};
}
hello('fn');
window.fn('success');

Variable value not gets updated if function has same name as variable (JavaScript) [duplicate]

This question already has answers here:
Why can variable declarations always overwrite function declarations?
(3 answers)
javascript hoisting: what would be hoisted first — variable or function?
(2 answers)
Closed 2 years ago.
I expected the answer to be "function text" as output, why the answer is 5?
var alpha = 5;
function alpha(){}
console.log(alpha);

Reference of variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Pass variables by reference in JavaScript
(16 answers)
Closed 3 years ago.
I would like to have on a separate script some logic and I need to let it know on which global variable all of my operations must be done: how can I pass to a function a reference to the variable?
function setVariable(variable){
//Here I let the script know on which variable it should work
//Example var x = reference to 'variable'
}
function run(){
if(x === 0){ ...
... }
}
I cannot "copy" the variable value because it's global and it can change anytime and this script has to read the updated value.

Losing class context when calling function by reference [duplicate]

This question already has answers here:
Javascript lost context when assigned to other variable
(4 answers)
Class methods assigned to variable (alias) lose the value of this in Javascript [duplicate]
(2 answers)
How to access the correct `this` inside a callback
(13 answers)
Why is JavaScript bind() necessary?
(4 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 3 years ago.
I've got a library that makes api calls to a number of sub-api's. If I call a function directly all works great:
let result = await apiLib.subApi1.getFoo();
But if I call by reference, I seem to lose the class context (i.e. 'this' is undefined within the class)
const endPoint = 'Foo';
let apiLibCall = apiLib.subApi1['get' + endPoint];
let result = await apiLibCall();
Is it possibly because of how 'await' works? Or am I calling by reference incorrectly?

JavaScript - Loop through required arguments list for a function [duplicate]

This question already has answers here:
How to get function parameter names/values dynamically?
(34 answers)
Inspect the names/values of arguments in the definition/execution of a JavaScript function
(5 answers)
javascript: get names of actual arguments passed in a function
(1 answer)
Closed 5 years ago.
This is a bit of a weird situation, but say I have the following function:
function myFunction(name, age, job){...}
Is there a way to get keys / loop through the required params for it (not the argument array passed to it), so it would be something like:
for(required_param_keys_by_myFunction){
console.log(key) // --> name, age, job
}

Categories

Resources