Advertisement
icw82

amalgamate() ver.1

Sep 13th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Первый элемент — основа
  3.  *  Второй — обновление
  4.  *  Возвращает объект из новых (изменённых) данных
  5.  *  До jQuery.extend далеко, но и этого хватает
  6.  */
  7. function amalgamate(){
  8.     if (!this.changes || window.changes)
  9.         var changes = {};
  10.  
  11.     if(arguments.length > 1){
  12.         for (var i in arguments[1]){
  13.             if ((typeof arguments[1][i] == 'object') && (typeof arguments[0][i] == 'object')){
  14.                 changes[i] = arguments.callee(arguments[0][i], arguments[1][i]);
  15.             }else{
  16.                 if (arguments[1][i] !== arguments[0][i]){
  17.                     arguments[0][i] = arguments[1][i];
  18.                     changes[i] = arguments[1][i];
  19.                 }
  20.             }
  21.         }
  22.  
  23.         return changes;
  24.  
  25.     }else
  26.         throw new Error('Недостаточно аргументов');
  27. }
  28.  
  29. // Тесты
  30.  
  31. var one = {a: 10, b: {x: 8, y: 5, z: 0}, c: 5}
  32. var two = {a: 10, b: {x: 8, y: 7}, d: 2}
  33.  
  34. var three = amalgamate(one, two);
  35.  
  36. console.log('amalgamate test 1: ' + (one.a == two.a));
  37. console.log('amalgamate test 2: ' + (one.b.x == two.b.x));
  38. console.log('amalgamate test 3: ' + (one.b.y == two.b.y));
  39. console.log('amalgamate test 4: ' + (one.d == two.d));
  40.  
  41. console.log('amalgamate test 5: ' + (three.b.y == two.b.y));
  42. console.log('amalgamate test 6: ' + (three.d == two.d));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement