JavaScript - ForEach - Stack Overflow

I have a JavaScript array of objects. Something like this:var people = [{ id:1, firstName: 'Joe�

I have a JavaScript array of objects. Something like this:

var people = [
  { id:1, firstName: 'Joe', lastName: 'Smith' },
  { id:2, firstName: 'Bill', lastName: 'Smith' }
];

I am iterating through the people using forEach. Here is my code:

function doSomething() {
  people.forEach(function(person, self) {
    self.helpPerson(person);
  }, this);
}

function helpPerson(person) {
  alert('Wele ' + person.firstName);
}

I am trying to call helpPerson from within the forEach loop. However, when I attempt to call it, I get an error that says:

TypeError: self.helpPerson is not a function

If I add console.log(self);, "0" gets printed to the console window. This implies that I'm not passing in my parameter correctly. Or, I'm misunderstanding closures (just when I thought I fully understood it :)).

So, why doesn't self exit?

I have a JavaScript array of objects. Something like this:

var people = [
  { id:1, firstName: 'Joe', lastName: 'Smith' },
  { id:2, firstName: 'Bill', lastName: 'Smith' }
];

I am iterating through the people using forEach. Here is my code:

function doSomething() {
  people.forEach(function(person, self) {
    self.helpPerson(person);
  }, this);
}

function helpPerson(person) {
  alert('Wele ' + person.firstName);
}

I am trying to call helpPerson from within the forEach loop. However, when I attempt to call it, I get an error that says:

TypeError: self.helpPerson is not a function

If I add console.log(self);, "0" gets printed to the console window. This implies that I'm not passing in my parameter correctly. Or, I'm misunderstanding closures (just when I thought I fully understood it :)).

So, why doesn't self exit?

Share Improve this question asked Dec 9, 2015 at 19:09 Some UserSome User 5,85716 gold badges56 silver badges96 bronze badges 1
  • what do you like to achieve with self? – Nina Scholz Commented Dec 9, 2015 at 19:16
Add a ment  | 

5 Answers 5

Reset to default 7

You don't need to invoke helpPerson with a this, self, or any other context. Just invoke it directly:

var people = [
  { id:1, firstName: 'Joe', lastName: 'Smith' },
  { id:2, firstName: 'Bill', lastName: 'Smith' }
];

function doSomething() {
  people.forEach(function(person) {
    helpPerson(person);
  });
}

function helpPerson(person) {
  alert('Wele ' + person.firstName);
}

When you log self to the console you are seeing "0" because it is printing the index of the loop iteration.

See the documentation for forEach to see what callbacks are passed to it's forEach function.

Typically, self is used to capture a context in a closure (var self = this;). Please see the related links to this question because that is a very important concept.

helpPerson is a global variable, not a property of the array.

self.helpPerson(person); should be helpPerson(person);

forEach passes two arguments to the callback: the item being iterated and its index. That's why console is logging 0.

You are expecting this to pass as an argument, when it's actually more magical than that. You can use this inside the callback and it will use the context of whatever this you passed as an argument to the forEach

function doSomething() {
  people.forEach(function(person, index) {
    this.helpPerson(person); //Or for brevity, you can just remove `this` here
  }, this);
}

function helpPerson(person) {
  alert('Wele ' + person.firstName);
}

forEach takes 2 parameters : a function(val, index, arr) and a this binding argument .

 people.forEach(function(person, self) {
    self.helpPerson(person); // self here would be array index number
  }, this);

the way you've defined helpPerson() you can call it directly like helpPerson(person);

Please see ments

var people = [
    { id:1, firstName: 'Joe', lastName: 'Smith' },
    { id:2, firstName: 'Bill', lastName: 'Smith' }
];

function doSomething() {
    people.forEach(function(person) { // no parameter for index required
        helpPerson(person);           // just call the function
    });                               // no other parameter is required
}

function helpPerson(person) {
    alert('Wele ' + person.firstName);
}

doSomething();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745621061a4636518.html

相关推荐

  • JavaScript - ForEach - Stack Overflow

    I have a JavaScript array of objects. Something like this:var people = [{ id:1, firstName: 'Joe�

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信