Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why angular.forEach context?

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?

like image 615
Lucas Avatar asked Dec 15 '15 16:12

Lucas


People also ask

How do you use foreach in angular?

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.

What is httpcontext in angular?

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.

What is *ngfor in angular?

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.

How to iterate through elements of an array in angular?

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.


2 Answers

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); 
like image 92
leo.fcx Avatar answered Oct 17 '22 00:10

leo.fcx


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.

like image 38
Chris Hermut Avatar answered Oct 17 '22 00:10

Chris Hermut