Prototypes and Prototypal inheritance in Javascript

The word prototype means an early model or a primary version of a machine or a product.All further version created from that prototype will inherit the properties of that prototype.Prototype-based programming is a style of object-oriented programming in which behaviour reuse is performed via a process of reusing existing objects via delegation that serve as prototypes.(Wikipedia). Nearly all objects in JavaScript are instances of Object which sits on the top of a prototype chain.The Object.prototype is on the top of the prototype inheritance chain. That means almost all the objects in JavaScript inherit the properties and methods from Object.prototype.JavaScript Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Prototype determines the behaviour of a data object.Properties including methods are inherited from the prototype.See the below array which inherits prototypes from Array.prototype.You can see is array,map,reduce etc which are inherited from Array.prototype Prototypal inheritance In Javascript a function is a first-class Object.All JavaScript objects inherit properties and methods from a prototype.

In the example above, function Person() is called object constructor function.Wth the new keyword,we can create new objects fusing the constructor function.

both myGo and myGoGo has properties first,last,age,eyecolor and  prototype We cannot add a new property to an Person directly.we can add it to its prototype.

Difference between a Framework and a Library : Javascript point of view

While attending interviews for programming jobs,one of the most common question you would face is to explain the difference between a libarary and a framework.Here in this article we look at the difference between a library and a framework from a Javascript point of view. Library Libraries provide methods/functions/modules  that can be useful while building your application.While using a library the control is with your code and you call the functions the libraries provide from within  your code.These functions are reusable and can be used at different parts of building your application.There are many Javascript libraries like JQuery,JqueryUI,moment.js etc.These libraries provides functions that serves different purposes.   So we can generally describe the working of a library as We call the library functions from from our code Flow Control is with our code                For example when using JQuery library, we include the library in our code and call the library functions from within our code in the following way

Framework A framework provides structure,resources for building your application.While using a framework you call your logic from within the framework .The control is with the framework code , your code is called when  logic is…

Share this page in social media platforms