Using outside variables inside an arrow function in Node.js [duplicate] - javascript

This question already has answers here:
How can I pass variable into an evaluate function?
(7 answers)
How do i return a value from page.evaluate() in puppeteer?
(3 answers)
Closed 1 year ago.
I'm kind of new to JavaScript but I'm using a function that takes another arrow function as a parameter. All I need to do in the function is simply set a value to some variable I have declared before it. From my understanding, var means it has global scope, so I'm not really sure why I cant use the variable. If I try to pass it in the parameter I get undefined. I'll paste the 2 lines of code giving me issues
var thing = events[i]["Address"];
await page.evaluate( () => document.querySelector('[class="form-control tt-input"]').value = thing)

Related

Does onclick = function put that function in a new scope? [duplicate]

This question already has answers here:
Why JavaScript function declaration (and expression)?
(5 answers)
Why use named function expressions?
(5 answers)
Closed 2 years ago.
Need your assistance. I have a function that was defined in onclick event and when i try to call it later there is an error that says function undefined.
author.onclick = function authorSlideInfo(event){
// do work
}
authorSlideInfo.then(() => {
// do work
}
Function returns a promise and i want to do something else after it ends. Is my function not in global scope? How to overcome this?

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 variable value lost when outside object [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
This may sound a newbie question but I'm having really hard time with variable scopes in Javascript.
I have the following JS snippet:
<script>
window.IDFVPlugin.getIdentifier(function(result){ uuid = result; });
alert(uuid);
</script>
I want to use the variable uuid anywhere in the script outside the window object. uuid returns the correct value only when inside the object and the value is lost when outside. So the alert above will log an undefined variable error.
You use a callback function. Result should be used inside of callback body. If you try to use it immediately after main function call - it will not be yet available
window.IDFVPlugin.getIdentifier(function(result){
uuid = result;
alert(uuid);
});

Javascript - what is the purpose of wrapping functions or codes in a function? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.

Categories

Resources