𝐈𝐟 𝐘𝐨𝐮 𝐊𝐧𝐨𝐰 𝐓𝐡𝐞𝐬𝐞 𝐑𝐮𝐥𝐞𝐬, 𝐘𝐨𝐮’𝐥𝐥 𝐍𝐞𝐯𝐞𝐫 𝐁𝐫𝐞𝐚𝐤 𝐂𝐨𝐝𝐞 𝐰𝐢𝐭𝐡 “𝐭𝐡𝐢𝐬” 𝐢𝐧 𝐣𝐚𝐯𝐚𝐬𝐜𝐫𝐢𝐩𝐭! 1. Global Context Outside any function, this refers to the global object window in browsers, global in js. 2. Regular Function (non-strict mode) this refers to the global object (window), even though you're inside a function. 3. Regular Function (strict mode) this is undefined. JavaScript stops the accidental global binding. 4. Object Method When a function is called as a property of an object, this refers to that object. 5. Arrow Function Doesn't have its own this. It inherits this from the surrounding lexical scope wherever the arrow function was written, not called. 6. Constructor Function / new keyword this refers to the newly created object being built. 7. Class Method Same as object method this refers to the instance of the class. 8. call(), apply(), bind() You manually set what this should be. These are the escape hatches. 9. Event Listeners (DOM) this refers to the HTML element that triggered the event but only with regular functions, not arrow functions. 10. Callback Functions this is often lost here. Passing a method as a callback detaches it from its original object. For more insightful content checkout below: 🟦 𝑳𝒊𝒏𝒌𝒆𝒅𝑰𝒏 - https://lnkd.in/dwi3tV83 ⬛ 𝑮𝒊𝒕𝑯𝒖𝒃 - https://lnkd.in/dkW958Tj 🟥 𝒀𝒐𝒖𝑻𝒖𝒃𝒆 - https://lnkd.in/dDig2j75 or Priya Frontend Vlogz 🔷 𝐓𝐰𝐢𝐭𝐭𝐞𝐫 - https://lnkd.in/dyfEuJNt #frontend #javascript #react #interview #interviewpreparation #SDE w3schools.com JavaScript Mastery JavaScript Developer freeCodeCamp
Nice share 👍
Rule #3 is exactly why 'Strict Mode' should be the default for everyone.🛡️ Preventing this from accidentally becoming the window object saves so many silent failures. Out of these 10 rules, which one was the 'lightbulb moment' that finally made JavaScript click for you?🧐
Thanks for tagging us and spreading the word! 🚀
Well done 👏 Priya Bagde Thanks for the mention.