Created
January 15, 2020 23:12
-
-
Save tomoima525/14ff32d5f8a7f046dbf2ca56f88d0ea9 to your computer and use it in GitHub Desktop.
arrow function to call this context
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
const materials = [ | |
'Hydrogen', | |
'Helium', | |
'Lithium', | |
'Beryllium' | |
]; | |
class Test { | |
test = (material) => { // test(material) would show error this.test2 is not a function | |
let m = material.length; | |
let n = this.test2(m); | |
return n; | |
} | |
test2 = (num) => { | |
return num + 2; | |
} | |
call() { | |
console.log(materials.map(this.test)); | |
// console.log(materials.map(this.test.bind(this))); would work if we want to call method | |
} | |
} | |
let test = new Test(); | |
test.call(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment