I was analyzing the angular.forEach
function:
Here is the code of angular.forEach
:
var values = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(values, function(value, key) {
this.push(key + ': ' + value);
}, log);
expect(log).toEqual(['name: misko', 'gender: male']);
The description of context is:
Object to become context (this) for the iterator function.
I came up with some questions:
Why use and when is useful the parameter context?!
Why use this instead the log variable?
angular.forEach(object_or_Array, iterator, [context]); Where the first parameter can be an object or an array. The second parameter is an iterator function that is invoked for each item in object or array. See the examples in the following section for learning more about forEach Angular function.
HttpContext is used to pass additional metadata to HTTP Interceptors in Angular. HttpContext is used to store additional metadata that can be accessed from HTTP Interceptors. Before this, there was no proper way to configure interceptors on a per request basis.
The *ngFor is a for loop in Angular that iterates through elements of an array, a list, or an object, and it can be used on template files to display a list of data on our web application or website.
We will introduce the .forEach () function in Angular with an example and use it to iterate through elements of an array. The .forEach () is a function in Angular that calls a function for each element in an array. It is not executed for empty arrays.
In the example that you provide, it is the same to use this
or directly the log array
. That is because callback
used in forEach method
and log array
are defined in same scope
.
However, there might be some cases in which that callback
is defined inside a different scope
than the log array
. Then the call should look like the following:
angular.forEach(values, getNames, log);
In this case, we should use this
inside the callback as it will refer to log array
which is defined in a different scope
.
EDIT:
See this JSFiddle demo and following code that shows what I explained.
var getNames = function(value, key) {
this.push(key + ': ' + value);
};
var processObject = function(){
var log = [];
// Now getNames callback is defined in a different
// scope than log
angular.forEach(values, getNames, log);
return log;
}
var values = {name: 'misko', gender: 'male'};
var resultArray = processObject(values);
// This gives same result as your exmple
alert(resultArray);
Never used angular.forEach() but it look's pretty simple.
var hello = {
message: 'Ok'
};
angular.forEach([' I', ' said', ' hello'], function (value) {
this.message += value;
}, hello);
console.log(hello.message);
1/ It will log 'Ok I said hello' so basically that 'context' param will allow you to update any object that you will refer. Use case is really broad...
2/ I think it has something to do with execution context aka. lexical environment so it meants that this
has to be related with how angular.forEach is implemented. Just check angularjs source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With