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);