THIS with Arrow Functions
const student = {
name: "abhii",
marks: 98,
// DIFFERENCE BETWEEN 'THIS' FOR A NORMAL FUNCTION AND ARROW FUNCTION
// for Normal function THIS-calling object
// for Arrow function THIS-lexical scope(parent)
getName: () => {
console.log(this);
return this.name;
},
getMarks: function () {
console.log(this);
return this.marks;
},
// THIS with arrow functions are very helpful for accessing inbuilt functions
getInfo1: function () {
setTimeout(() => {
console.log(this);
}, 2000);
},
getInfo2: function () {
setTimeout(function () {
console.log(this);
}, 3000);
},
};
student.getName();
student.getMarks();
student.getInfo1();
student.getInfo2();

Comments
Post a Comment