JS Object Prototypes šŸŽÆ

beginner
9 min

JS Object Prototypes šŸŽÆ

Welcome to this comprehensive guide on JavaScript Object Prototypes! We'll dive deep into understanding what prototypes are, why they're essential, and how to work with them. By the end of this tutorial, you'll have a solid foundation to create sophisticated JavaScript objects.

Table of Contents šŸ“

  1. Understanding Objects in JavaScript
  2. Introducing Prototypes
  3. Every Object's Shared Parent: Object.prototype
  4. Creating Custom Prototypes
  5. Inheritance and Prototype Chain
  6. Modifying the Prototype Chain
  7. Quirks and Best Practices
  8. Challenge: Prototype Practice šŸ’”

1. Understanding Objects in JavaScript

In JavaScript, objects are collections of key-value pairs (properties). They are a fundamental data structure used to store and manipulate data in a more organized and flexible manner than primitive data types.

javascript
const myObject = { name: 'John', age: 25, greet: function() { console.log(`Hello, I am ${this.name}`); } };

In the example above, myObject is an object with three properties: name, age, and greet.


2. Introducing Prototypes

Every object in JavaScript inherits from an invisible object called the prototype. This prototype object contains properties that are shared among all objects.

The prototype of an object can be accessed using the Object.getPrototypeOf() method.

javascript
const myObject = { // ... }; const prototype = Object.getPrototypeOf(myObject); console.log(prototype); // Output: { constructor: ʒ Object() }

The prototype of all objects is Object.prototype, which contains various predefined properties and methods.


3. Every Object's Shared Parent: Object.prototype

Object.prototype is the shared parent object for all JavaScript objects. It contains properties and methods like toString(), hasOwnProperty(), constructor, and more.

For example, when you call the toString() method on an object, it will return a string representation of the object.

javascript
const myObject = { // ... }; console.log(myObject.toString()); // Output: [object Object]

šŸ’” Pro Tip: Remember that all JavaScript objects inherit from Object.prototype.


4. Creating Custom Prototypes

You can create custom prototypes for your objects to share common properties and methods across multiple objects.

javascript
const PersonPrototype = { greet: function() { console.log(`Hello, I am ${this.name}`); } }; const john = Object.create(PersonPrototype); john.name = 'John'; const jane = Object.create(PersonPrototype); jane.name = 'Jane'; john.greet(); // Output: Hello, I am John jane.greet(); // Output: Hello, I am Jane

In the example above, we created a PersonPrototype object with a greet method. Then, we created two objects, john and jane, using Object.create() with PersonPrototype as the prototype. Both objects inherit the greet method from the prototype.


5. Inheritance and Prototype Chain

When accessing properties on objects, JavaScript follows a prototype chain. If a property is not found in the object itself, JavaScript looks for it in the object's prototype. This continues until it reaches the top of the prototype chain, which is Object.prototype.

javascript
const myObject = Object.create(Object.prototype); myObject.property = 'hello'; console.log(myObject.property); // Output: hello console.log(myObject.hasOwnProperty('property')); // Output: true

In the example above, we created an object myObject and added a property property. Since myObject's prototype is Object.prototype, we can see that it has the hasOwnProperty() method from the prototype.


6. Modifying the Prototype Chain

You can modify the prototype chain by changing the prototype of an object or by adding new properties to the prototype. Be cautious when doing this, as it can lead to unexpected behavior if not handled properly.

javascript
const myObject = Object.create(Object.prototype); myObject.sayHi = function() { console.log('Hi there!'); }; Object.prototype.greet = function() { console.log('Hello, I am an object!'); }; myObject.greet(); // Output: Hello, I am an object! myObject.sayHi(); // Output: Hi there!

In the example above, we modified the greet method for all objects in the prototype chain. Now, when calling greet on myObject, it outputs the new greet message instead of the original one from Object.prototype.


7. Quirks and Best Practices

While prototype-based inheritance is powerful, it can lead to unexpected behavior if not managed properly.

  1. Avoid modifying built-in prototypes: Built-in JavaScript objects like Array and String have predefined prototypes that are essential for their functionality. Avoid modifying these prototypes to prevent unintended consequences.

  2. Use ES6 classes for simple inheritance: If you need to create complex classes with multiple inheritance, you can still use prototypes. However, for simple inheritance, consider using ES6 classes for better readability and maintainability.


8. Challenge: Prototype Practice šŸ’”

Put your newfound knowledge to the test by implementing the following tasks:

  1. Create a Person constructor with properties name, age, and a greet method.
  2. Create a Student constructor that extends the Person constructor with a grade property.
  3. Write a function that takes an object and returns its prototype chain, showing the properties at each level.

Good luck and happy coding! šŸ¤–šŸš€