Today, I reviewed the Object Oriented and Prototypal Inheritance section in Tony Alicea's JavaScript: Understanding the Weird Parts course.
Conceptual Aside: Classical vs. Prototypal Inheritance
Inheritance: One object gets access to the properties and methods of another object.
Classical Inheritance is what's best known. It's a way of sharing in popular languages such as Java and C#.
Prototypal Inheritance is simple, flexible, extensible, and easy to understand.
Understanding the Prototype
All objects in JavaScript (including functions) have a proto object property. Each object can have its own prototype, proto object.
They look like they are on the main object but they are simply on the “prototype chain” which has to do with having access to other objects, propotypes, and methods.

var person = { firstname: 'Default', lastname: 'Default', getFullName: function() { return this.firstname + ' ' + this.lastname; } } var john = { firstname: 'John', lastname: 'Doe' } // don't EVER do this! for demo purposes only!! // this is a visual of how john can inherit the // methods of person john.__proto__ = person;
Everything is an Object (or a primitive)
Every object, array, or function we create in JavaScript has a prototype (i.e. {}.__proto__ = Object) which already has methods that exists on it.
Reflection and Extend
Reflection – An object can look at itself, listing and changing its properties and methods.
for (var prop in john){ if (john.hasOwnProperty(prop)){ console.log(prop + ": " + john[prop]); } }
Leave a Reply