benbria / object-spy

0 stars 0 forks source link

Isolate the original object from the wrapper object #2

Closed bllanos closed 9 years ago

bllanos commented 9 years ago

Current Behaviour

The object to be observed, passed as the parameter to watch() in src/watch/watch.coffee, is modified through the wrapper object returned by watch(), because it still serves as the source for the current values of properties.

Furthermore, modifications made to the original object are not the same as those which appear to be applied by client code to the wrapper object. For example, when the client retrieves a property of the wrapper which is of type 'object', the wrapper replaces the corresponding property on the original object with a wrapper of the property's value. If the wrapper was replaced with the original object, the same property retrieval would not result in a property assignment.

Therefore, the original object is neither protected from modification by the library, nor updated to mirror the apparent changes applied to the wrapper object. Either of these behaviours would make sense and potentially be useful bases for future work.

Tentative Solution

When wrapping an object, a complete copy of the object should be produced and used instead of the original object for keeping track of property values, etc.

The procedure used for cloning the original object should be selected carefully. It should:

Candidates include

bllanos commented 9 years ago

All cloning methods considered do not copy non-enumerable properties.

So, to avoid introducing defects arising from incomplete cloning algorithms, I am suspending this issue for now.

For further reading, see http://www.2ality.com/2012/08/underscore-extend.html