Skip to content

Instantly share code, notes, and snippets.

@optimistanoop
Created January 14, 2017 07:10
Show Gist options
  • Save optimistanoop/8a1ed1b5bef01e5dfa426b94253df196 to your computer and use it in GitHub Desktop.
Save optimistanoop/8a1ed1b5bef01e5dfa426b94253df196 to your computer and use it in GitHub Desktop.
Prototype chain in Js
// Simple copy or deep copy of any object to other is just a copy of available properties of object to the other.
// Prototypical copy or chaining of any object is done by Object.create() method.
var a = {a:1};
var b = Object.create(a); // now b will have all properties of a and future proprties of a too.
// Where as in prototypical chain , child object delegates all the request for the properties to the parent
// which are not found in the object.
// It works like the inherited parent Object, property lookups are only delegated to parent object only when it is
// not found in child object.
// We can override properties of the parent object.
// Helps in removing code duplication.
// All objects inherit Object prototype. which has toString(), hasOwnProperty() etc.
// All Arrays inherit Array prototype , which has some overriden methods of Object prototype. i.e - toString() for arrays
// are diffrent.
// Hence Arrays inherit Array prototype which inherits Object prototype.
// Hence any lookup in a normal array will look for the property in the array, if not found then in Array prototype(imediate parent)
// , and again if it is not found then in Object prototype (super parent).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment