Prototypal Classes

When you want your program to improve performance – use prototyping chains.  Where any objects made to delegate its failed property to another object.  In other words if an object lack a property, it delegates that property to it’s prototype object.  Use .prototype rather than extend.

Delegating Relationships -> Object literals ->does NOT allow to prototype.

Constructor properties, allow us to skip the very expensive step of having to copy properties over when the new object is built.  So the steps for making a class in a prototypal pattern:

  • Function to make instances – a line which generates a new instance object
    • a delegation from the new object to the same prototype object.
  • Logic for argumenting the object with properties which define as unique from the other object.

Whenever a function is created, it will have an object attached to it, which can be used as a container for methods, in case you plan on using that function to build instances of a class.

NOTE – Not stored at the key .method but at the .prototype.

Prototypes affect the in-memory model. Using the .prototype over .method is purely cosmetic.  If it does not delegate to .method it is the same for .prototype.  Resolved objects in the function only delegate to the .prototype due to the call.  So the prototype is there to store not with any additional characteristics.

The .prototype is confusing.  Every prototype object has a .constructor property which points back to the function it cane attached to.  So a mutual link between any new function and it’s companion .prototype object.  All instances of a class delegate through failed lookups their prototype.  Therefore they have the same constructor function.

The Instance of Operator checks if the right operands .prototype object can be found anywhere in the left operands prototype chain.

To summarise Prototypal pattern – function sharing via a prototype delegation is the goal.

Leave a Reply

Your email address will not be published. Required fields are marked *