Calling the normal function of "this" [duplicate] - javascript

This question already has answers here:
The invocation context (this) of the forEach function call
(5 answers)
Why "this" refers to Window in forEach in javascript? [duplicate]
(8 answers)
Using `this` in a forEach() [duplicate]
(1 answer)
Accessing this in a forEach loop results in undefined
(3 answers)
Closed 2 years ago.
In JavaScript, this depends on the function call method.
Calling General Functions
Method Call
Call constructor function
arrow function call, etc.
but in this case
class Numbers {
numberArray = [];
multiply(arr) {
arr.forEach(function(item) {
this.numberArray.push(item * item);
});
}
}
const numbers = new Numbers();
numbers.multiply([1, 2, 3]);
If you look at the fourth line in this class example,
Since arr.forEach is calling a callback function with this, I think the arr is a this but Why does this means undefined?
I don't know why it's a general function call.

Related

setInterval and how it interacts with objects [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
JavaScript setInterval and `this` solution
(9 answers)
How do JavaScript closures work?
(86 answers)
Closed 10 months ago.
I've noticed when using setInterval() with objects, it accepts this:
setInterval(function(){auto1.increaseValue(s);}, auto1.interval);
Where auto1 is an object from a class.
However, the program throws an error when I put an object or object of an array of another object inside the function.
Ex.
let index = this.arrayForItems.length-1;
setInterval(function(){this.arrayForItems[index].increaseValue(s);header.innerHTML = s.value;}, this.arrayForItems[index].interval);
setInterval(function(){this.item.increaseValue(s);header.innerHTML = s.value;}, this.item.interval);//Where item is the object inside object shop
But this works.
let index = this.arrayForItems.length-1;
let referenceToPurchase = this.arrayForItems[index];
setInterval(function(){referenceToPurchase.increaseValue(s);header.innerHTML = s.value;}, referenceToPurchase.interval);
Why is this so?

What is the meaning of returning a function from another function? [duplicate]

This question already has answers here:
Functions that return a function
(10 answers)
How do JavaScript closures work?
(86 answers)
Closed 1 year ago.
What is the meaning of returning a function from another function in javascript. Is this is same as if I call a function which is in another function
function fun(c){
let a=3;
return function(){
return a+c;
}
}
let add=fun(2);
console.log(add);
. And my second question is why this add variable is a function.
Currently, the object which you are returning is the function.
In this context, variable add contains a reference to the function which you have to invoke.
This is called currying. It allows you to apply arguments to functions one by one.
Example:
const add = a => b => a + b // Function that takes `a` and returns a function that adds `a` to `b`
const addThree = add(3) // bind `a` to 3. This is a function that takes `b` and adds 3 to it
console.log(addThree(1)) // now `b` is 1, it's gonna be added to `a` and return the final value
There's a lot of literature about currying and partial application. Google is your friend

Uncaught TypeError is not a function when I call a method from another method using setInterval in Javascript [duplicate]

This question already has answers here:
setTimeout and "this" in JavaScript
(5 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 2 years ago.
I'm having this error when I call a method from another method in a class. This method is been called from setInterval.
class App {
b() {
console.log("BBBBBBBB")
}
t() {
console.log("TTTTTTT")
this.b();
}
}
const t = new App();
setInterval(t.t, 1000);
You need to bind the method to the variable so the value of this stays constant. Read this page for more information.
setInterval(t.t.bind(t), 1000);

Don't understand a construct in a JS Decorator [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 3 years ago.
I was studying on JS Decorators and don't understand how the wrapper is able to access the inner functions argument. The snippet works, but I don't understand why the anonymous function 'function(val)' gets access to val, the argument of slow().
// https://codepen.io/lideebe/pen/VOjGvb
// simple function that gets wrapped
function slow(x){
return x * 3;
}
// the wrapper function
function cacheDecorator(func){
return function(val){ // How does this anonymous function get access to val?
return func(val)
}
}
// do the wrap
slow = cacheDecorator(slow);
// call the function
console.log(slow(2));
Output is 6 and that is correct.
It's because cacheDecorator returns a function which takes a parameter val, and when you call that returned function with the value 2, it just gets accessed like normal. So slow, after you reassign it, is:
slow = function(val) {
return val * 3;
}
So val is used as the parameter for the argument passed when the newly wrapped function is called.

How can I pass a value from one function to another function which has object and without declaring a global variable in JS? [duplicate]

This question already has answers here:
Passing one variable from one function to another function in java script phone gap [closed]
(2 answers)
How can I pass a value from one function to another function without declaring a global variable in JS?? [duplicate]
(7 answers)
Closed 9 years ago.
I just want to pass the value from my new1 and new new2 function to new3 function, where new1 and new2 containing object..
function new1(obj1){
var a = obj1.x;
}
function new2(obj2){
var c=obj2.y;
}
function new3(){
if(a<c){
dosomething();
}
}
You already have access to the properties in question since they must be passed to the first two functions. There is no need to operate on the private a and c variables.
function new1(obj1){
var a = obj1.x;
}
function new2(obj2){
var c=obj2.y;
}
function new3(obj1,obj2){
if(obj1.x < obj2.y){
dosomething();
}
}
new1(obj1);
new2(obj2);
new3(obj1,obj2);

Categories