Skip to main content

Command Palette

Search for a command to run...

π”π§ππžπ«π¬π­πšπ§ππ’π§π  𝐩𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐚π₯ 𝐒𝐧𝐑𝐞𝐫𝐒𝐭𝐚𝐧𝐜𝐞

Updated
β€’2 min readβ€’View as Markdown
π”π§ππžπ«π¬π­πšπ§ππ’π§π  𝐩𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐚π₯ 𝐒𝐧𝐑𝐞𝐫𝐒𝐭𝐚𝐧𝐜𝐞
M

hey there πŸ‘‹ i'm Samia, a Backend developer turning to frontend world. i used to work as a Laravel and PHP developer for 3 and half years, in that journey, i had to deal from time to time with some frontend tasks, like fixing responsiveness or converting some psd files into HTML & CSS, that was when i fall in love with it.

I loved the idea of turning creative and beautiful design into reality, so i made that career switch and now turning fully to Frontend development.

Feel free to checkout my journey on Instagram or just pass Hi

as developers we deal with objects most of the time, but have you ever wondered how JavaScript objects can access a treasure trove of built-in methods, seemingly by magic? Let's demystify it!

Take this as an example: 𝒄𝒐𝒏𝒔𝒕 𝒖𝒔𝒆𝒓 = { π’π’‚π’Žπ’†: "𝑱𝒐𝒉𝒏" };
πŸ€” It appears as though the object has access to various methods even though we didn't explicitly create them, for example, we can access 𝒖𝒔𝒆𝒓.π’‰π’‚π’”π‘Άπ’˜π’π‘·π’“π’π’‘π’†π’“π’•π’š(π’π’‚π’Žπ’†), even though we did not create it in user, it still works!

How is that possible? The key lies in understanding 𝐩𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐚π₯ 𝐒𝐧𝐑𝐞𝐫𝐒𝐭𝐚𝐧𝐜𝐞.

πŸ› οΈ The truth is, when we create an object, JavaScript sets up a prototype chain for that object. Each object gets a hidden [[𝐏𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐞]] property, This property holds a reference to its parent object. If it's a nested object, it references its immediate parent. If it's a top-level object like user, it references the πŽπ›π£πžπœπ­ object.

πŸ‘‰ when is the prototype useful? When you access a property or method on an object, and that property or method doesn't exist on the object itself, JavaScript will look for it in the object referenced by its [[𝐏𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐞]] property.

in the attached example, you notice that we have a π’ˆπ’–π’†π’”π’• object, that inherits from a 𝒖𝒔𝒆𝒓 object
When you call π’ˆπ’–π’†π’”π’•.π’ˆπ’“π’†π’†π’•(), JavaScript looks for the greet method on the π’ˆπ’–π’†π’”π’• object. Since it doesn't find the method there, it proceeds to check the prototype, which is the 𝒖𝒔𝒆𝒓 object.
JavaScript finds the greet method on the 𝒖𝒔𝒆𝒓 object, and it executes that method, producing the output: "Hello, my name is Sammy.".

For more details check out: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes

More from this blog

Untitled Publication

5 posts

PHP & Laravel developer switching to Frontend realm, i enjoy writing blogs and sharing my knowledge, follow my instagram to see my journey to becoming a frontend developer