Skip to content

Instantly share code, notes, and snippets.

@TrevorJTClarke
Created May 13, 2015 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TrevorJTClarke/09d4914a442a135b898c to your computer and use it in GitHub Desktop.
Save TrevorJTClarke/09d4914a442a135b898c to your computer and use it in GitHub Desktop.
A study into accessing "this" within the new lexical this, and arrow functions.
// Test array
var evens = [2,4,6,8,10]
// Example Expression
var nums = evens.map((v, i) => {
// here, "this" becomes undefined. There is no lexical parent to tie to.
console.log(this)
return v + i
})
var parent = {
name: "parent",
// new object literal syntax!! :)
get(){
// here, "this" has a lexical parent to tie to. We can access "this".
console.log("get()",this)
},
getNums( numbers ){
return numbers.map((v, i) => {
// here, "this" has the same lexical parent as its containing function.
console.log("getNums parent",this)
return v + i
})
}
}
// The final result
console.log(nums)
console.log(parent.get())
console.log(parent.getNums( nums ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment