1

I created an object with object constructor ,made a prototype function.But when I called that function 'this' keyword not referring to object

function Person(name,age,location){
this.name = name;
this.age = age;
this.location = location;
}

var person1 = new Person('john',22,'Pakistan');

Person.prototype.greet = ()=>{
  console.log(this.name + "says Hi");
};

person1.greet();

But when I used simple anonymous function ,the problem gets solved.

Person.prototype.greet = function(){
   console.log(this.name + "says Hi");
};

Why it's not working with arrow function?

ShahEryar Ahmed
  • 115
  • 1
  • 2
  • 10
  • 1
    there `this` is `window`. – Jai Dec 21 '18 at 13:49
  • Also potentially relevant: https://stackoverflow.com/questions/22939130/when-should-i-use-arrow-functions-in-ecmascript-6 https://stackoverflow.com/questions/28798330/arrow-functions-and-this – VLAZ Dec 21 '18 at 13:50

0 Answers0