Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading