Skip to content

Instantly share code, notes, and snippets.

@tomoima525
Created January 15, 2020 23:12
Show Gist options
  • Save tomoima525/14ff32d5f8a7f046dbf2ca56f88d0ea9 to your computer and use it in GitHub Desktop.
Save tomoima525/14ff32d5f8a7f046dbf2ca56f88d0ea9 to your computer and use it in GitHub Desktop.
arrow function to call this context
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