Answers for "why can't you use arrow function with this in javascript for declaring methods"

11

arrow function javascript

// Normal Function in JavaScript
function Welcome(){
  console.log("Normal function");
}

// Arrow Function
const Welcome = () => {
  console.log("Normal function");
}
Posted by: Guest on May-22-2021
0

arrow function javascript

const power = (base, exponent) => {
  let result = 1;
  for (let count = 0; count < exponent; count++) {
    result *= base;
  }
  return result;
};

//if the function got only one parameter

const square1 = (x) => { return x * x; };
const square2 = x => x * x;

// empty parameter

const horn = () => {
  console.log("Toot");
};
Posted by: Guest on December-08-2020
0

why can't you use arrow function with this in javascript for declaring methods

'use strict';

var obj = { // does not create a new scope
  i: 10,
  b: () => console.log(this.i, this),
  c: function() {
    console.log(this.i, this);
  }
}

obj.b(); // prints undefined, Window {...} (or the global object)
obj.c()
Posted by: Guest on July-16-2021

Code answers related to "why can't you use arrow function with this in javascript for declaring methods"

Code answers related to "Javascript"

Browse Popular Code Answers by Language