tags:

views:

29

answers:

1
function associate( obj1 ,obj2 , key ){
    if(typeof key === 'object'){
        for ( var i in key )
            associate(obj1, obj2 , key[i]);
    } else {
        obj1.watch(key, function (id, oldval, newval) {
                obj2[id] = newval;
                return newval;
        });
    }

} 

I wrote this function because if have to different obj and both need to be updated. What do you think about this function?

  obj = { key1: 'value2', key2: 'value2'} 
  obj2 =  {key1: 'value2', key2 : 'value2'}

  associate( obj ,obj2 , 'key1' )

  obj.key1 = 'new value';

  console.log(obj.key1) 
  --> 'new value'

  console.log(obj2.key1) 
  --> 'new value'
A: 

It won't work. At all.

return obj1[id] = obj2[id] = newval;

That in particular isn't going to get you anywhere.

Feel free to use http://www.jslint.com/ should you want to find issues in your code.

Meke
its working, thats not the problem, the question is, is this good practice?
Sep O Sep
Byt the fact that it, in theory(which we all know isn't the same as practice) shouldn't work - I'm going to say no, it isn't.
Meke
okay, i changed to 'obj2[id] = newval; return newval;' should it now work even in theory?
Sep O Sep
There are no errors now. So, yes, it works, albeit confusing. But that's just because it's out of context
Meke