So I'm basically just trying to add watchers to Javascript's Getter/Setter functions but having no luck yet.
KnockoutJS (http://knockoutjs.com) does this with .track()
.
A YouTube video example of track()
in use:
https://www.youtube.com/watch?v=MNiUcuo3Wio
This leads me to my first question, does __defineGetter__
and __defineSetter__
only apply to objects and not string literals?
Here's a code example of how this would ideally work, it may clarify some things.
// Knockout-like tracker in NodeJS.
function track(trackObj, callback) {
if (!trackObj) return;
trackObj.__defineCallback__ = callback;
trackObj.__defineGetter__('value', function() {
return 'something';
});
trackObj.__defineSetter__('value', function() {
console.log("fn called");
if (trackObj.__defineCallback__)
trackObj.__defineCallback__("new value");
});
}
var something = "track me!";
track(something, function(newValue) {
console.log("Something has changed to: " + newValue);
});
something = "I have changed."; // Trigger the update callback function.