this refer to inside a regular function?In a regular function, this refers to the object that called the function. If called alone, it can be undefined in strict mode or the global object otherwise.
this behave inside an arrow function?Inside an arrow function, this does NOT have its own value. It uses this from the surrounding (parent) scope where the arrow function was created.
this?Arrow functions keep the this from the outer scope, so you don’t have to use tricks like var self = this or bind(). This makes code simpler and easier to understand.
this?Arrow functions should NOT be used as methods because this will not refer to the object but to the outer scope. This usually causes unexpected behavior.
Example: What will this code print?<br><pre>const obj = {<br> value: 42,<br> regularFunc: function() {<br> console.log(this.value);<br> },<br> arrowFunc: () => {<br> console.log(this.value);<br> }<br>};<br>obj.regularFunc();<br>obj.arrowFunc();</pre>The regularFunc prints 42 because this refers to obj. The arrowFunc prints undefined because this refers to the outer scope (likely the global object or undefined in strict mode) which has no value.
this inside an arrow function refer to?Arrow functions do not have their own this. They use the this from where they were created.
Arrow functions do not bind this to the object, so this is from the outer scope.
this?Arrow functions inherit this from the surrounding scope, which helps keep the correct context in callbacks.
const obj = {<br> count: 10,<br> increment: function() {<br> setTimeout(() => {<br> console.log(this.count);<br> }, 100);<br> }<br>};<br>obj.increment();The arrow function inside setTimeout uses this from increment, which is obj. So it prints 10.
Arrow functions do not bind this to the global object; they inherit it from the parent scope.
this behaves differently in arrow functions compared to regular functions.this.