Guest User

Untitled

a guest
Jan 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. var obj = {};
  2. obj.a = 1; // fire event, property "a" added
  3.  
  4. var checkObj = (function(watchObj)
  5. {
  6. var initialMap = {},allProps = [],prop;
  7. for (prop in watchObj)
  8. {
  9. if (watchObj.hasOwnProperty(prop))
  10. {//make tracer object: basically clone it
  11. initialMap[prop] = watchObj[prop];
  12. allProps.push(prop);//keep an array mapper
  13. }
  14. }
  15. return function()
  16. {
  17. var currentProps = [];
  18. for (prop in watchObj)
  19. {
  20. if (watchObj.hasOwnProperty(prop))
  21. {//iterate the object again, compare
  22. if (watchObj[prop] !== initialMap[prop])
  23. {//type andvalue check!
  24. console.log(initialMap[prop] + ' => ' watchObj[prop]);
  25. //diff found, deal with it whichever way you see fit
  26. }
  27. currentProps.push(prop);
  28. }
  29. }
  30. //we're not done yet!
  31. if (currentProps.length < allProps.length)
  32. {
  33. console.log('some prop was deleted');
  34. //loop through arrays to find out which one
  35. }
  36. };
  37. })(someObjectToTrack);
  38. var watchInterval = setInterval(checkObj,100);//check every .1 seconds?
Add Comment
Please, Sign In to add comment