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

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?

Related

How can I use the setTimeout() statement? [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
How can I pass a parameter to a setTimeout() callback?
(29 answers)
What is the difference between a function call and function reference?
(6 answers)
Closed 3 months ago.
I was Coding Today (new) and i was wondering how people made js and python so i tried making a function based programming language using js
function init(){
log(console,"lmao ok")
setTimeout(log(console,'#--- Done'), 5000 )
}
function log(arg1,arg2) {
// body...
if (arg1 == console){
console.log(arg2)
}
else {
return
}
}
function prompt(arg1,arg2,arg3){
console.debug
var arg1 = "lmao"
}
init()
log(console,'tosay')

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

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)

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 - 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.

check if a javascript function is exist or is defind [duplicate]

This question already has answers here:
How to tell if a JavaScript function is defined
(23 answers)
Closed 9 years ago.
I want to know whether a function is exist or is defined before i call it.
I have this line of code:
function renderView(){
// some code
}
You can check the typeof, it returns undefined if it's not defined or out of scope, and function if it's a function
if ( typeof renderView == 'function' ) {
// it exists
}

Categories

Resources