What is the difference between these two prototype in JS?

35 Views Asked by At

code 1

var User1 = function (name, age) {
    this.name = name;
}
User1.prototype.getName = () => { return this.name; }
var x = new User1("gladiator");
console.log(x.getName());

Output = undefined

code 2

var User1 = function (name, age) {
    this.name = name;
}
User1.prototype.getName = function () { return this.name; }
var x = new User1("gladiator");
console.log(x.getName());

Output = gladiator

0

There are 0 best solutions below