I’m using mootools 1.4.3 for basic class inheritance. I’m actually converting some code from a different Class library. This other class library would dynamically create getters and setters based on a predefined function name template (a member function _get_xxx would yield a getter for xxx). I wanted to do something similar with mootools. I’m not new to Javascript, but I’m somewhat unfamiliar with the more advanced concepts.
I have gotten it to work partially. In the code where the Class object is being defined I loop through the classes parameters and add defineGetter or defineSetter accordingly.
This works great and it creates the getters and setters properly. Where it fails is when I create a child class. These dynamic getters/setters are not propagated to the child class. I’m not sure if this is possible and if so how it is done. Using the above change to the mootools Class and these classes:
In the log for foo I can see the getter and setter for bar. In the log for baz I can see the getter and setter for quux, but I’d also like to see the getters and setters from the parent. Again, if I can get this to work, it will save me a lot of time in converting this code.
Right. I think I get what you are trying to do but…
__defineGetter__and__defineSetter__are deprecated and not cross browser: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/DefineSetterProtecting and proxying direct access to these properties won’t work when you extend the object because the setter/getter – accessing the properties on the new object – won’t go to the parent (prototype object) where you are listening.
you can try looking at the
Extendsparam. when you set the new Class up, look for the functions being present all the way up the prototype chain from the new params object and if a match is found, you should apply the setter/getter to the new object as well. it’s not that easy…I’d say, probably after:
newClass.prototype.parent = parent;you should be able to check if the getter/setters have become available and attach your listeners. you probably want to save a reference somehow unless you want to check all class properties at this point.alternatively, you can refactor the initialize class mutator w/o going into the type creation and append your stuff there – though the original object may not be available.