Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. Object.observe(i, function(b){ b.object[change.name] > b.type ? console.log('decreased') : console.log('increased'); });
  2.  
  3. var i = 4;
  4. var a = i;
  5. setInterval(function()
  6. {
  7. if(i != a) a > i ? console.log('decreased') : console.log('increased');
  8. }, 1000);
  9. i = 10;
  10.  
  11. var i = {i:5};
  12. i.watch("i", function (id, oldval, newval) {
  13. newval > oldval ? console.log('decreased') : console.log('increased');
  14. return newval;
  15. });
  16.  
  17. if (!Object.prototype.watch) {
  18. Object.defineProperty(Object.prototype, "watch", {
  19. enumerable: false
  20. , configurable: true
  21. , writable: false
  22. , value: function (prop, handler) {
  23. var
  24. oldval = this[prop]
  25. , newval = oldval
  26. , getter = function () {
  27. return newval;
  28. }
  29. , setter = function (val) {
  30. oldval = newval;
  31. return newval = handler.call(this, prop, oldval, val);
  32. }
  33. ;
  34.  
  35. if (delete this[prop]) { // can't watch constants
  36. Object.defineProperty(this, prop, {
  37. get: getter
  38. , set: setter
  39. , enumerable: true
  40. , configurable: true
  41. });
  42. }
  43. }
  44. });
  45. }
  46.  
  47. // object.unwatch
  48. if (!Object.prototype.unwatch) {
  49. Object.defineProperty(Object.prototype, "unwatch", {
  50. enumerable: false
  51. , configurable: true
  52. , writable: false
  53. , value: function (prop) {
  54. var val = this[prop];
  55. delete this[prop]; // remove accessors
  56. this[prop] = val;
  57. }
  58. });
  59. }
  60.  
  61. i = 4;
  62.  
  63. function setValue(obj, val)
  64. {
  65. val < obj ? console.log('decrease') : console.log('increase');
  66. obj = val;
  67. }
  68.  
  69. setValue(i, 10);
  70.  
  71. var triggerVariable;
  72.  
  73. function changeVariable(newVal){
  74. if(triggerVariable !== newVal){
  75. triggerVariable = newVal;
  76. callFunction();
  77. }
  78. }
  79.  
  80. function callFunction(){
  81. alert('variable changed!');
  82. }
  83.  
  84. changeVariable(10);
  85.  
  86. function specVal (value)
  87. {
  88. this.onChange = null;
  89. this.value = value;
  90. this.val = function()
  91. {
  92. return this.value;
  93. };
  94.  
  95. this.add = function(i)
  96. {
  97. this.value += i;
  98. if (this.onChange != null)
  99. this.onChange(this);
  100. return this;
  101. }
  102. }
  103.  
  104. function somethingChanged(val)
  105. {
  106. alert('changed: ' + val.val());
  107. }
  108.  
  109. myValue = new specVal(10);
  110. myValue.onChange = somethingChanged;
  111. myValue.add(5);
  112. myValue.add(4);
  113.  
  114. Function setMyVar(val){
  115. if(_myVar !== val){
  116. _myVar = val;
  117. $('#anyelement').triggerHandler('myvarupdated', [myVar]);
  118. }
  119. }
  120.  
  121. var lastValue = 0;
  122. $(function () {
  123.  
  124.  
  125. if (lastValue > originalValue){
  126. console.log ("Increasing");
  127. }
  128. else if (lastValue < originalValue) {
  129. console.log("Decreasing");
  130. }
  131. lastValue = originalValue;
  132.  
  133. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement