Skip to main content

Prototype Inheritance

·1 min

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}