Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript: Mozilla .watch() functionality for all properties of an object (possibly cross-browser)
    text
    copied!<p><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Object/watch" rel="nofollow noreferrer">https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Object/watch</a></p> <p>The .watch() method does this in short: "Watches for a property to be assigned a value and runs a function when that occurs."</p> <p>Long descriptive form: "Watches for assignment to a property named prop in this object, calling handler(prop, oldval, newval) whenever prop is set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modified newval (or by returning oldval)."</p> <p>There is a question for getting it to work in all browsers here: <a href="https://stackoverflow.com/questions/1029241/javascript-object-watch-for-all-browsers">Object.watch() for all browsers?</a></p> <p>I am looking for something similar to that. What I'm looking for is a method I can use to fit this specification: "Watches for assignment to any property in this object and runs a function when that occurs." The main difference is that it is any property, and just any specific property.</p> <p>Can somebody create such a method or if they know such a method is already in existence, link to it? It'd be useful to have it work in all browsers (minus IE, or minus IE8 if IE9 conforms)</p> <p>Edit: For an example of what I mean, I'll show what I need it for.</p> <pre><code>var DiscreteLine = function (leftBound, length){ this.positive = []; this.negative = []; this.length = length; this.leftBound = leftBound; this.rightBound = leftBound + length if (this.leftBound &lt; 0){ this.negative.length = (leftBound * -1) + 1; } else { this.negative.length = 0; } if (this.rightBound &gt;= 0){ this.positive.length = rightBound + 1; } else { this.positive.length = 0; } this.watchObject = new ObjectWatcher(handler(prop, oldval, newval){ /* some stuff */ }); } </code></pre> <p>Then, when for example, if somebody did the following:</p> <pre><code>theLine = new DiscreteLine(-2, 4); theLine[-8] = 10; </code></pre> <p>The handler would call, with the arguments ("-8", undefined, 10). (What would end up happening is, is that the script would recalculate leftBound and length properties automatically (like how Arrays automatically update the length property).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload