Bindings in Arrow Function

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 193:- Bindings in Arrow Function

Arrow functions in JavaScript are a shorthand way of writing function expressions. They are commonly used in modern JavaScript programming due to their concise syntax and the way they handle the this keyword.

In an arrow function, this refers to the this value of the enclosing lexical context, unlike regular functions where this can be bound to different values based on the way the function is called. Arrow functions also have a simpler syntax than regular functions and automatically return the result of the expression they contain.

Regarding bindings in arrow functions, it's important to note that arrow functions do not have their own this, arguments, super, or new.target bindings. Instead, they use the this value of the enclosing lexical context, which means that arrow functions can be used to preserve the value of this from the surrounding code.

For example, consider the following code:

javascriptCopy code

const person = {  name: 'John',  sayHi: function() {    console.log(`Hi, my name is ${this.name}`);  } }; person.sayHi(); // Output: Hi, my name is John

In the above code, this inside the sayHi function refers to the person object. If we were to use an arrow function instead, this would refer to the this value of the enclosing lexical context, which would be the global object in this case.

However, if we use an arrow function to define sayHi as follows:

javascriptCopy code

const person = {  name: 'John',  sayHi: () => {    console.log(`Hi, my name is ${this.name}`);  } }; person.sayHi(); // Output: Hi, my name is undefined

In the above code, this inside the arrow function refers to the this value of the enclosing lexical context, which is the global object. This means that this.name is undefined, since the global object does not have a name property.

21. Closures

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?