Javascript prototypes

Created By: chatGPT

JavaScript is a prototype-based programming language, which means it relies on prototypes instead of classes for inheritance. Each object can serve as a prototype for another object. This allows for dynamic extension of objects and reuse of properties and methods across different objects.
Creating an Object Prototype: You can create an object prototype by defining a constructor function and adding properties or methods to the prototype object.
function Person(name, age) {
    this.name = name;
    this.age = age;
}

Person.prototype.greet = function() {
    console.log(`Hello, my name is ${this.name}`);
};
In this example, we created a Person constructor that initializes name and age. We added a greet method to Person.prototype, allowing all instances of Person to use this method.
Instantiating an Object: You can create an instance of the Person object and invoke the greet method as follows.
const john = new Person('John', 30);
john.greet(); // Outputs: Hello, my name is John
Inheritance via Prototypes: You can also create a new object that inherits from an existing prototype. For example, you can create a Employee constructor that inherits from Person.
function Employee(name, age, title) {
    Person.call(this, name, age);
    this.title = title;
}

Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;

Employee.prototype.describe = function() {
    console.log(`${this.name} is a ${this.title}`);
};
Here, we use Object.create to set up inheritance and preserve the prototype chain. Now, Employee instances can also use methods from the Person prototype.
Using the Inherited Object: We can instantiate the Employee object and call its methods just like we did with Person.
const jane = new Employee('Jane', 28, 'Developer');
jane.greet(); // Outputs: Hello, my name is Jane
jane.describe(); // Outputs: Jane is a Developer
Prototype Chain: Remember that JavaScript checks the prototype chain when you access properties. If a property is not found in the object itself, it looks up the prototype chain until it finds the property or reaches the end of the chain.
Introduction And SetupVariablesData TypesMethodsControl FlowFunctionsOperatorsData StructuresCommentsSyntaxEvent HandlingError HandlingPrimitive Data TypesReference Data TypesConstLetVarStringNumberBooleanNullUndefinedSymbolObjectArrayTemplate LiteralsArrow FunctionsRegular FunctionsCallApplyBindAsync FunctionsPromisesCallback FunctionsIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor EachBreak StatementContinue StatementTry CatchThrowFinallyScopeClosureThis KeywordPrototypesInheritanceConstructor FunctionsClass SyntaxStatic MethodsGettersSettersSpread OperatorRest OperatorDestructuring AssignmentMap MethodFilter MethodReduce MethodEvent ListenersDom ManipulationInnerHTMLQuerySelectorAddEventListenerPreventDefaultStopPropagationTimeoutsIntervalsJsonLocal StorageSession StorageRegexMath ObjectDate ObjectString MethodsArray MethodsObject MethodsDeep CloningShallow CloningCallback HellPromise ChainingAsync AwaitMap Vs ForEachFilter Vs ReduceNullish Coalescing OperatorOptional ChainingTypeof OperatorInstanceof OperatorStrict EqualityLoose Equality