Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const obj = {
  2.     x: 5,
  3.     func: function() { console.log(this.x); }, //wyświetli 5
  4.     arrowfunc: () => { console.log(this.x) } //wyświetli undefined
  5. }
  6.  
  7. const array = [1, 2]; //jakaś tablica, żebym mógł pętlę forEach na niej wykonać
  8. const object = {
  9.     x: 5,
  10.     func: function() { //tutaj musi być zwykła funkcja, aby skorzystać w środku z this.
  11.         array.forEach( () => {
  12.             console.log(this.x); //wyświetli 5 5, a jeśli zamienilibyśmy tę arrow function (w środku forEacha) na zwykłą to byłoby undefined
  13.         });
  14.     }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement