0

So, I saw several examples that this should work. But obviously, I'm missing something, because it doesn't. :/

Could somebody please explain to me what I'm doing wrong here? :)

function Code (b) {
  this.b = b
  this.arr = []
}

Code.prototype.add = (v) => {
  console.log(this.b)
  this.arr.forEach(element => {
    console.log(element)
  });
  this.arr.push(v)
}

var c = new Code('bla')
console.log(c.add('asdf'))

So this throws an error with:

this.arr.forEach(element => {
  ^

TypeError: Cannot read property 'forEach' of undefined

Obviously, I'm doing something wrong here. But I don't know what.

Thanks! Gergely.

Hannibal
  • 1,078
  • 2
  • 12
  • 24
  • 3
    arrow (or rocket) functions `this` is not the same as regular functions `this` - in general, arrow functions are not the right pattern for an objects prototype functions – Jaromanda X Dec 08 '17 at 05:31
  • 1
    I agree with @JaromandaX. Change your code from this: `Code.prototype.add = (v) => {` to this `Code.prototype.add = function(v) {` That will correct the `this` pointer. Using an arrow function *forces* the `this` pointer to be what it was at the time the function was declared. – Intervalia Dec 08 '17 at 05:33
  • use ```function(element)``` => – Manoj Jadhav Dec 08 '17 at 05:41

2 Answers2

2
function Code (b) {
  this.b = b
  this.arr = []
}
Code.prototype.add =function(v){
  console.log(this.b)
  this.arr.forEach(function(element){
    console.log(element)
  });
  this.arr.push(v)
  console.log(this.arr)
}

var c = new Code('bla')
console.log(c.add('asdf'))

You should use function(), instaed of () => arrow fucntion, since arrow function's this works differently.
this of an arrow function only refers to this that exists in the outer scope.

moon
  • 640
  • 6
  • 14
  • 2
    Just a pointer, it is a bad practice to answer a duplicate. If you know the issue, you are more likely to form better search query and find a match. You should look for such posts and mark post as duplicate. I also see, you do not have enough rep for it, so you should add the link in comment and someone will vote. Lets try to keep the portal clean. Have a good day :-) – Rajesh Dec 08 '17 at 05:37
  • @Rajesh Thank you for your kind explanation on how to use this site well! – moon Dec 08 '17 at 05:46
  • DAMN IT! I KNOW I tried this. :( That's why I didn't see for the duplicate. And nooow of course it works. Like... Wth man. :( Anyways. Thank you! :) – Hannibal Dec 08 '17 at 06:39
-1

function Code (b) {
  this.b = b
  this.arr = []
}

Code.prototype.add = function(v) {
  console.log(this.b)
  this.arr.forEach(element => {
    console.log(element)
  });
  this.arr.push(v)
}

var c = new Code('bla')
console.log(c.add('asdf'))
Madhavan.V
  • 825
  • 1
  • 6
  • 14
  • 2
    Just a pointer, it is a bad practice to answer a duplicate. If you know the issue, you are more likely to form better search query and find a match. You should look for such posts and mark post as duplicate. I also see, you do not have enough rep for it, so you should add the link in comment and someone will vote. Lets try to keep the portal clean. Have a good day :-) Also, an answer without explanation is incomplete. – Rajesh Dec 08 '17 at 05:37
  • Okay I understood. I will follow that next time. Thank you and wish you the same. :) – Madhavan.V Dec 08 '17 at 05:41