最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why angular.forEach context? - Stack Overflow

programmeradmin3浏览0评论

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 bee 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?

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 bee 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?

Share Improve this question edited Dec 15, 2015 at 17:20 Amirhossein Mehrvarzi 19k7 gold badges50 silver badges73 bronze badges asked Dec 15, 2015 at 16:56 LucasLucas 1,2914 gold badges17 silver badges34 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

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.

发布评论

评论列表(0)

  1. 暂无评论