javascript - Is there any value to redefining objects fields or array elements instead of assigning reference to other object -


for example, have simple implementation of angular's data binding mechanism - notification when data on object changes:

// implementation var watchers = [];  function watch(fn, cb) {     var oldvalue = fn();     watchers.push(function() {         var newvalue = fn();         if (newvalue !== oldvalue) {             oldvalue = newvalue;             cb(newvalue);         }     }) }  function digest() {     watchers.foreach(function(fn) {         fn()     }); }  // using implementation var = {     prop: 3 }  watch(function() {     return some.prop; }, function(newvalue) {     console.log('value changed ' + newvalue); }); 

now have 2 options of modifying some

// option 1 delete some.prop; // optional here some.prop = 5; digest(); // outputs "value changed 5"  // option 2  // instead of changing property on same object, // assign reference new object = {     prop: 11 } digest(); // outputs "value changed 11" 

my question of 2 options better use?
objects contain lot of fields, reassing them in option 1 not convenient. however, using second option i'm loosing reference original object , potentially lead memory leaks if had reference object. thinking correct?

why creating implementation core functionality in angularjs? should use built in feature. way data gets propigated through scopes other thing in angular makes concerned none issue.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

delphi - Indy UDP Read Contents of Adata -