vl - inheritance

Overview of Object Oriented Programming

  • Key concept: Inheritance

    • Grouping features and reusing code.

    • Analogy: Children inheriting traits from parents.

    • Example: A programmer inherits characteristics of a person and adds more.

Inheritance in JavaScript

  • Multiple methods of achieving inheritance.

  • Focus on the default method:

    • Called prototype chaining.

    • Concept: Setting the prototype to a particular object for behavior and property inheritance.

Prototype Chaining

  • Setting the prototype involves:

    • Inheriting all behaviors and properties from the specified prototype object.

Overriding Methods

  • Example: Overriding the toString method.

    • Specific implementation provided to avoid defaulting back to the prototype's implementation.

    • toString now returns the name property of the object.

Shape Examples

  • Defined shapes:

    • Function Shape:

    • Has a property called name.

    • Overrides the toString method to return its name.

    • 2D Shape:

    • No override of toString; inherits it from Shape.

    • Triangle Shape:

    • Takes parameters side and height.

    • Properties and behaviors added to represent a triangle.

Inheritance Implementation

  • Procedure for making 2D Shape inherit from Shape:

    • Set the prototype of 2D Shape to the Shape object.

    • Use constructor new 2D Shape to create an instance.

    • Results in inheritance of the toString method from Shape.

Prototype Chain Behavior

  • Explanation of how prototype chain checks work:

    • First checks the current object (e.g., 2D Shape).

    • If property not found, checks the prototype (e.g., Shape).

    • Retrieves name property if it belongs to the Shape prototype.

Triangle Instance Creation

  • Creating an instance of Triangle:

    • Example given for instantiation.

    • States the potential for using prototype properties/methods.

Moving Properties to Prototypes

  • Guidelines on when to move properties to a prototype:

    • Typical creation of object with constructor function uses this to add own properties.

    • Each instance has its own property, which consumes memory.

Memory Efficiency

  • Key factor: If properties have the same value across instances, move property to the prototype.

    • Example: If every shape has the same name, it is inefficient to have unique instances.

    • Instead, define the property on the prototype: Shape.prototype.name = 'shape';

Shared Values

  • Aim to share values to save memory:

    • Properties should be defined on prototype when shared by all instances.

  • Methods should be generally defined on prototype for shared access between object instances.

  • Properties defined within the constructor using this when unique to instances.

Conclusion

  • Summary of concepts covered related to inheritance in JavaScript.

  • Reinforces efficiency of using prototype for shared properties and benefits of object-oriented design principles.

  • Closing remarks for further engagement in the next session.