setInterval and how it interacts with objects [duplicate] - javascript

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?

Related

this keyword in an object i am returning from a function is undefined [duplicate]

This question already has answers here:
Methods in ES6 objects: using arrow functions
(6 answers)
Arrow Function in Object Literal [duplicate]
(1 answer)
Closed 24 days ago.
image showing the function i created
the hello function of the returned object logs out undefined, instead of reference to the reply object or the returned object and I don't know why
I haven't really tried anything.

Calling the normal function of "this" [duplicate]

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.

empty an array passed to a function [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I am trying to empty an array passed to a function within javascript:
var a = [1,2,3];
function emptyArrayNo(ar) {
// the following does not empty the global array
ar = [];
}
function emptyArrayYes(ar) {
// any of the following 3 methods empties the global array
ar.length = 0;
//ar.splice(0, ar.length)
//while(ar.length > 0) {ar.pop();}
}
emptyArrayNo(a);
console.log(a); // returns [1,2,3]
emptyArrayYes(a);
console.log(a); // returns []
All the three methods within emptyArrayYes() seem to work, which I think is due to the reference to the array passed not being modified within the function. However when we set the array to [] within the function, I believe the reference changes therefore causing issues i.e. not being seen at the global scope. Can someone please clarify what is going on here? Thanks.

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

Javascript array value not printing [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Access object properties within object [duplicate]
(2 answers)
Closed 6 years ago.
How to do ElementName get value from ElementId?
var Elements = {
ElementId: '#myelement',
ElementName: Elements.ElementId
};
alert(Elements.ElementName);
You haven't created the object yet, so it's not going to be defined. You can define that property on the object once it's created like this:
var Elements = {
ElementId: '#myelement'
};
Elements['ElementName'] = Elements.ElementId;
alert(Elements.ElementName);
However I think there's a better way; what specifically are you using the name and id properties for?

Categories