Skip to content

Instantly share code, notes, and snippets.

@medikoo
Last active December 17, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medikoo/5602644 to your computer and use it in GitHub Desktop.
Save medikoo/5602644 to your computer and use it in GitHub Desktop.
DB setPrototypeOf case

setPrototypeOf use case for Database engine written in JavaScript

Ability to rebuild DB state in unspecified order

e.g. first set property of an object and then set object's instance type, or set object as a value of given property and in later turn deciding of what instance that object is.

In short we need the ability to create the object before knowing it's instance type. It makes it very safe for any race conditions. Thanks to that behavior, in engine I work with, rebuild of data state from localStorage is done in whatever order iteration of keys is done (where each key describes a single property and not whole object definition). There's no need to watch the order, or mind circular references, it just works.

Proto change possibility in that case solves a lot of problems and opens many doors. I worked before with simpler version of such engine, which didn't rely on proto switching, it was much more error prone. Missing links crashed (or blocked) database, also order in which state is re-created was crucial. It demanded an extra hacking to get desired result, which is not the case with current version.

Ability to change type of existing object, to more specific one

e.g. switch from Dog to Shepherd.

Apart of database engine case good example of using that would be working with DOM elements, e.g. I got an already created <ul> element, and have a custom extension to UL class (JS speaking to UL.prototype), I want this <ul> element to get the characterstics of ULMoreSpecific which extends UL, so I change <ul>'s prototype to ULMoreSpecific.prototype. This is the way I provide custom methods to elements in domjs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment