Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davedub/e6d88ebdbea640bfbb55ad986a1f3393 to your computer and use it in GitHub Desktop.

Select an option

Save davedub/e6d88ebdbea640bfbb55ad986a1f3393 to your computer and use it in GitHub Desktop.
Arrow function to push array into new array created by davedub - https://repl.it/@davedub/Arrow-function-to-push-array-into-new-array
// solution for "writing arrow functions" exercise
// https://repl.it/student/classrooms/27654
const example1 = {
stuff: [1, 2, 3, 4, 5, 6, 7],
otherBits: [],
addStuff: function() {
this.stuff.forEach(function(thing) {
this.otherBits.push(thing)
})
}
}
// example1.addStuff()
// console.log(example1.otherBits)
// returns error: "this.otherBits undefined"
const example2 = {
stuff: [1, 2, 3, 4, 5, 6, 7],
otherBits: [],
addStuff: function() {
this.stuff.forEach((i) => this.otherBits.push(i)
)}
}
example2.addStuff()
console.log(example2.otherBits)
// [ 1, 2, 3, 4, 5, 6, 7 ]
// => undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment