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?
Related
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)
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?
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)
This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 3 years ago.
Have a loop, there i have a instance which I will sometime set a method eqvialent to a object which change something in that object. But when I try, it says it is undefined.
var a = [];
a.push(
{
x:0
func: function(){this.x++; console.log(this.x);}
}
);
doFuncInLoop = a[0].func;
doFuncInLoop();
The console log will say this.x i undefined and I understand it not refer to the object, it refer to doFuncInLoop.x, but I want it to change a[0].x
This question already has answers here:
Maintaining the reference to "this" in Javascript when using callbacks and closures
(3 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 5 years ago.
I am trying to create a constructor for my Shelf class that sets its' shelfObjects instance variable to the results of a PostgreSQL query. The problem is that 'this' no longer refers to the instance of the class once we reach the callback function. How would I go about solving this problem in the right way?
class Shelf {
constructor(displayNum, sideItemNum, callNum){
this.displayNum = displayNum;
this.sideItemNum = sideItemNum;
this.callNumber = callNumber;
var query = db.query("MY POSTGRESQL QUERY", function(err, result){
this.shelfObjects = result.rows;
});
}
}