0

I'm trying to understand the difference between the 3 methods below. What is the difference between CreateUser2 and CreateUser3.

I'm unable to console log the name variable in CreateUser2 outside of the function but it worked inside of the declaration. CreateUser3 works fine, is there a reason to why arrow functions work differently?

function CreateUser (name)
{
    this.name = name;
}

const CreateUser2 = (name) =>
{
    this.name = name;
    console.log(this.name);
}

const CreateUser3 = function(name)
{
    this.name = name;
}

let user = new CreateUser('umer');
let user2 = CreateUser2('umer');
let user3 = new CreateUser3('umer');

console.log(user.name);
//console.log(user2.name);
console.log(user3.name);
umer raja
  • 1
  • 2
  • 1
    Does this answer your question? [Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?](https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-exchangeable) – Titulum Mar 16 '20 at 10:44
  • Heads up!,, version 2 is not the same, and will create a global var called `name`.. – Keith Mar 16 '20 at 10:52

0 Answers0