medikoo / dbjs

In-Memory Database Engine for JavaScript
MIT License
28 stars 4 forks source link

Rethink automatic descriptor resolution #32

Open medikoo opened 9 years ago

medikoo commented 9 years ago

/cc @kamsi

Currently when doing obj.getDescriptor('x') we'll always get some descriptor. If property was never defined, we'll get base descriptor that is ancestor for each descriptor in a database (a base descriptor prototype)

It's dangerous, as when we try to do obj.getDescriptor('x').someSetting = value we may accidentally set this value for every property in a system.

Currently as an alternative there's obj.getOwnDescriptor('x') which by all means will return descriptor for given object (if it didn't exist, it's created and returned). Still it's dirty, as it creates objects we may not need. It's also usually not used, as in most cases we call getDescriptor on instances to get descriptor of prototype properties.

I see three possible solutions at the moment.

  1. Make getOwnDescriptor (or create alternative method) so it returns own descriptor only if it exists, and returns null otherwise. It will quickly expose eventual bugs.
  2. Do not return base descriptor (of $ id), from getDescriptor call, so it's accessible via public API.
  3. Freeze base descriptor (of $ id) (via Object.freeze) so any changes on it are forbidden.