开发者

linking two objects // updating value on first and second

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.lo开发者_如何学运维g(obj.key1) 
  --> 'new value'

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜