Search code examples
javascriptfunctionobjectecmascript-6arrow-functions

Arrow Function Created In An Object Returns undefined When Called Using .call() Method


i just created an object with an arrow function :

const user={
    fName:"0xN1nja",
    about:()=>{
        return this.fName;
    }
}

now, when i call that arrow function with .call() method, it returns undefined :

a=user.about.call(user);
console.log(a);
>>> undefined

But when i replaced that arrow function with a normal function expression, it returns the value

const user={
    fName:"0xN1nja",
    about:function(){
        return this.fName;
    }
}
a=user.about.call(user2);
console.log(a);
>>> 0xN1nja

what is happening here?

PS : im new to javascript


Solution

  • Arrow functions don't bind to this as function do. That's one of the main reason they were introduced (probably the most important one).

    You cannot even bind them.