Skip to content

Instantly share code, notes, and snippets.

@optimistanoop
Created January 14, 2017 06:57
Show Gist options
  • Save optimistanoop/a86389eb835c82dee7e55a463436d412 to your computer and use it in GitHub Desktop.
Save optimistanoop/a86389eb835c82dee7e55a463436d412 to your computer and use it in GitHub Desktop.
Prototype chains example in Js
var red = {a:1};
var blue = extend({},red);
console.log(blue.a); // 1
red.b = 2;
console.log(blue.b); // undefined
var green = Object.create(red);
console.log(green.a); // 1
console.log(green.b); // 2 var coming from prototypical chains after lookup to parent object
red.c= 3;
console.log(green.c); // 3 , var coming from prototypical chains after lookup to parent object
green.c = 4;
console.log(green.c); // 3 , overriden var , js will not lookup to parent for it.
// Here , object created by Object.create aggrees to delegate the new property added in parent obj to the child obj
// by which red.c = 3 is delegated to green . Prototypical chain in js is one of the example of inheritence in js.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment