Created
May 13, 2015 19:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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