JavaScript Object Inheritance and the Property Chain

$images[0]['name']

In JavaScript you will often hear that everything is an object, but I can and other developers argue that this is not 100% true. In JavaScript everything is related with how we understand objects and primitive values

Object Inheritance and Prototype chaining

In JavaScript you will often hear that everything is an object, but I can and other developers argue that this is not 100% true. In JavaScript everything is related with how we understand objects and primitive values. As already I discuss in my courses JavaScript provides different data types to hold different types of values inside the variables. There are two data types in JavaScript:

Two Different Data Types

  • Primitive values
  • Non-primitive values (object references)
  • So the Data types that are known as primitive values in JavaScript are:

    • numbers.
    • strings.
    • booleans.
    • null.
    • undefined

    Objects such as functions and arrays are referred to as non-primitive values The fundamental difference between those two mentioned above is that primitives are: not mutable and non-primitive are: mutable.
    Everthing else that is not primitives falls into objects

    • Arrays.
    • Functions.
    • Dates.
    • Objects.
    • Wrappers for numbers, Boolean and Strings

    Object-Oriented Programming

    Object-Oriented Programming relay on using objects, methods, properties, and objects in JavaScript or any other programming language interact through methods and their properties. Now since objects were introduced we can have much more cleaner code, more structured applications into separate modules and also we can use objects to store data.

    Now in ES5 we have a constructor/prototype functions

    For Example, a Constructor Person can have many different instances as per the picture below. Here the constructor is a blueprint with its parameters defined and the rest of the instances that are objects can use and access all of this from the constructor. constructor and instances

    Inheritance — the ES5 way

    While classes are just icing sugar over ES5, we must know how actually inheritance is implemented in ES5. This is why I created a course from scratch so you can learn from the basics. When it comes to inheritance, the only important thing to understand this example so we have for example Person Constructor and Administrator, which will be an instance of the Perso. Now in Person, we define the basic parameters and methods that are relevant to the each and every person. In the Administrator, we define its own methods and parameters but what is important in Inheritance we can access those methods, properties from the Person and that is less code, cleaner design and ect.

    Inheritance

    Inheritance — With Prototype Property

    Inheritance in JS is possible with the prototype property. In JavaScript, every object has a prototype and we also know that the prototype of an object is also an object, thus creating nesting of prototypes. So, JavaScript uses a similar concept for creating an inheritance by chaining multiple instances to prototypes and creating nested prototypes.

    That is all for now I will link here another post to the ES6 and you will be able to see the difference between ES5

About TheodoreCoding

TheodoreCoding is site where we publish all of our web development articles, courses and YouTube Content. This content is owned by Rick Sekuloski, which is owner of this site and the courses and Youtube Channel. Here people can find relevant information regarding any news from IT world, latest web development techniques and etc. Our goal is simple to create fun and interesting content for our viewers. Here you can find udemy coupons for some courses or promotions.