When I add a normal function to the prototype, 'this' pointing is right,but when i use arrow function,the 'this' pointing is messive ,just like below picture
what is the 'this' pointing in the prototype function when i use arrow function?
function Person(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
if (typeof Person._initialized === 'undefined') {
Person.prototype.getName = function() {
console.log(this);
};
Person._initialized = true;
}
}
let person1 = new Person('12', 11, '123');
let person2 = new Person('122', 11, '1223');
person1.getName();
person2.getName();
function Person2(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
if (typeof person2._initialized === 'undefined') {
Person2.prototype.getName = ()=> {
console.log(this);
};
Person2._initialized = true;
}
}
let person11 = new Person2('12', 11, '123');
let person22 = new Person2('122', 11, '1223');
person11.getName();
person22.getName();