`this` is undefined when used in a Destructuring Parameter function [duplicate] - javascript

This question already has answers here:
Javascript OOP - lost this in asynchronous callback
(6 answers)
Javascript callbacks losing 'this'
(3 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 3 years ago.
I have this function inside my class which has Destructuring parameters
export default class MainCtrl {
constructor(mainService ) {
'ngInject';
this.mainService = mainService;
}
$onInit() {
navigator.geolocation.getCurrentPosition(this.search);
}
search({ coords }) {
console.log(this); // shows undefined
this.mainService.search(coords);
}
}
Now when im using this.mainService it says this is always undefined. How to pass the this instance on this kind of function?

Related

repeat function with parameter by settimeout inside itselft [duplicate]

This question already has answers here:
Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()?
(5 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 2 years ago.
I need to iterate a function with its argument but got the recursion error
Error in mounted hook: "InternalError: too much recursion"
the function:
methods: {
showSlides: function(n) {
console.log(n);
setTimeout(this.showSlides(n), 3000);
//setTimeout(this.showSlides(n++), 3000);
}
},

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?

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?

Can I make a object selreference method, when I have done that equal to another variabel [duplicate]

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

How to call JavaScript class's method from another method? [duplicate]

This question already has answers here:
'this' keyword overriden in JavaScript class when handling jQuery events
(2 answers)
es6 classes and "this" with event handlers [duplicate]
(2 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 5 years ago.
I am using iOS's JavaScriptCore framework but I am having difficulty getting a JavaScript (es6) class to call another method. Here is my playground code.
do {
let j = JSContext()!
j.evaluateScript("class Base123 { method1(arg1) { return this.method2(arg1) } method2(arg1) { return {\"asdf\":\"123\"} } }")
j.evaluateScript("var b = new Base123(); var handler = b.method1")
j.evaluateScript("handler(4)")?.toDictionary() //<-- expected ["asdf": "123"]
}
Essentially I am Creating a class called Base123 with two methods. method1 calls method2. However the call to method2 is undefined.
Does anyone know how to do this?
Reformatting the class definition to make it easier to read:
class Base123 {
method1(arg1) {
return this.method2(arg1)
}
method2(arg1) {
return {"asdf":"123"}
}
}
Is there something wrong with this javascript definition?
Try replacing var handler = b.method1 with var handler = b.method1.bind(b).

Categories

Resources