|
| 1 | +## ⚑ Objects: |
| 2 | + |
| 3 | +Objects are generally [Non-Primitive Data Types](./data-types.md#-non-primitive-data-types). |
| 4 | + |
| 5 | +Objects in JavaScript are collections of key-value pairs, where the keys are properties and the values can be any data type. They represent real-world entities or concepts. |
| 6 | + |
| 7 | +*Objects are commonly used to represent the data structures, encapsulate properties and methods, and model real-world entities. They are essential for building complex real-word JavaScript applications.* |
| 8 | + |
| 9 | +### ☴ Overview: |
| 10 | +1. [Object properties and methods](#-object-properties-and-methods) |
| 11 | +2. [Object creation](#-object-creation) |
| 12 | +3. [Constructors](#-constructors) |
| 13 | +4. [Methods](#-methods) |
| 14 | +5. [Properties](#-properties) |
| 15 | +6. [Inheritance](#-inheritance) |
| 16 | +7. [Polymorphism](#-polymorphism) |
| 17 | +8. [this keyword](#-this-keyword) |
| 18 | +9. [Prototype inheritance](#-prototype-inheritance) |
| 19 | + |
| 20 | +### ✦ Object Properties and Methods: |
| 21 | +Properties: Key-value pairs that define the characteristics of an object. |
| 22 | +Methods: Functions defined within an object that operate on its properties. |
| 23 | + |
| 24 | +### ✦ Object Creation: |
| 25 | +- **Literal notation:** |
| 26 | +```javascript |
| 27 | +let person = { |
| 28 | + name: "kumar", |
| 29 | + age: 30, |
| 30 | + greet: function() { |
| 31 | + console.log("Hello!"); |
| 32 | + } |
| 33 | +}; |
| 34 | +``` |
| 35 | + |
| 36 | +- **Constructor function:** |
| 37 | +```javascript |
| 38 | +function Person(name, age) { |
| 39 | + this.name = name; |
| 40 | + this.age = age; |
| 41 | +} |
| 42 | + |
| 43 | +let person = new Person("kumar", 30); |
| 44 | +``` |
| 45 | + |
| 46 | +### ✦ Constructors: |
| 47 | +It is one type of functions used to create objects. It typically use the this keyword to initialize object properties. |
| 48 | + |
| 49 | +### ✦ Methods: |
| 50 | +It is one type of functions defined within an object. It can access and modify the object properties. |
| 51 | + |
| 52 | +### ✦ Properties: |
| 53 | +It is a key-value pairs that store data within an object. |
| 54 | + |
| 55 | +### ✦ Inheritance: |
| 56 | +Inheritance is a mechanism in object-oriented programming that allows to inherit properties and methods from one object (parent or base object) to another object (child or derived object). |
| 57 | + |
| 58 | +*Syntax:* |
| 59 | +```javascript |
| 60 | +class ChildClass extends ParentClass { |
| 61 | + // Constructor |
| 62 | + constructor() { |
| 63 | + super(); // Call the parent class's constructor |
| 64 | + // Additional properties or methods |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +```javascript |
| 70 | +class Employee { |
| 71 | + constructor(name) { |
| 72 | + this.name = name; |
| 73 | + } |
| 74 | + |
| 75 | + details() { |
| 76 | + console.log("Details of Employee"); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class Manager extends Employee { |
| 81 | + details() { |
| 82 | + console.log("Name: " + this.name); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +let manager = new Manager("Kumar"); |
| 87 | +manager.details(); |
| 88 | +``` |
| 89 | + |
| 90 | +### ✦ Polymorphism: |
| 91 | +Polymorphism is the ability of objects to respond to the same method call in different ways. |
| 92 | + |
| 93 | +*Syntax: (Overriding methods)* |
| 94 | +```javascript |
| 95 | +class ChildClass extends ParentClass { |
| 96 | + // Override the parent class's method |
| 97 | + method() { |
| 98 | + // Custom implementation |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +```javascript |
| 104 | +class Employee { |
| 105 | + constructor(name) { |
| 106 | + this.name = name; |
| 107 | + } |
| 108 | + |
| 109 | + details() { |
| 110 | + console.log("Details of Employee"); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class Manager extends Employee { |
| 115 | + details() { |
| 116 | + console.log("Name: " + this.name); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +let manager = new Manager("Kumar"); |
| 121 | +manager.details(); |
| 122 | +``` |
| 123 | + |
| 124 | +### ✦ this Keyword: |
| 125 | +This refers to the current object within a method. |
| 126 | + |
| 127 | +### ✦ Prototype Inheritance: |
| 128 | +Prototype Inheritance means that objects created using a constructor function inherit properties and methods from the constructor prototype. |
| 129 | + |
| 130 | +**Prototype:** An object that serves as a template for other objects. |
| 131 | + |
| 132 | +**Creating custom prototypes:** |
| 133 | +*Syntax: (Modifying the prototype)* |
| 134 | +```javascript |
| 135 | +Object.prototype.myCustomMethod = function() { |
| 136 | + // Method implementation |
| 137 | +}; |
| 138 | +``` |
| 139 | + |
| 140 | +*Syntax: (Creating a custom prototype object)* |
| 141 | +```javascript |
| 142 | +function MyObject() {} |
| 143 | + |
| 144 | +MyObject.prototype.myProperty = "value"; |
| 145 | +MyObject.prototype.myMethod = function() { |
| 146 | + // Method implementation |
| 147 | +}; |
| 148 | +``` |
| 149 | + |
| 150 | +--- |
| 151 | +[⇪ To Top](#-objects) |
| 152 | + |
| 153 | +[❮ Previous Topic](./dom-manipulation.md)   [Next Topic ❯](./arrays.md) |
| 154 | + |
| 155 | +[⌂ Goto Home Page](../README.md) |
0 commit comments