+++ +++

Classical Inheritance vs Prototype Inheritance

  • Classical Inheritance: like C#, Java…
  • Prototypal Inheritance: Javascript

Prototype

  • Prototype chain: the links that Javascript can search from current object to __proto__ parent object (and so on)

share same object (memory)

Obj

prop-1

proto

prop-2

proto

prop-3

Obj2

...

Everthing is an Object (or a primitive)

Javascript Engine plug

Javascript Engine plug

Javascript Engine plug

proto

proto

Object

function

Array

proto: Object-builtin
{toString: f,..}

proto: Funcion-builtin
{call:f,..}

proto: Array-builtin
{push:f,...}

Reflection and Extend

  • Reflection: An object can look at itself, listing and changing its properties and methods
  • Extend: some kind of composition by copied properties and method of source project to destinition. In Underscore.js we can user _.extend(des, source1, source2...)

John
{f1, f2}

Jane
{f3}

Jim
{f4, f5}

extend

John
{f1, f2, f3,f4, f5}