-
Custom "getter setter"
Hi,
Im building custom class properties and its working fine:
var myDiv = document.createElement('div');
myDiv.__defineGetter__("_x", function() { return this.style.left; });
myDiv.__defineSetter__("_x", function(x) { this.style.left = x+'px'; });
but... only in Mozilla type browsers. It doesn't work in Safari! Arrrg!
Doesn anyone know a hack?
Thanks!
P.S. "myDiv = {_x get(...) , _x set(...)};" method doesn't work at all for divs...
-
myDiv.someName = function(){alert("hello");}
-
Defining getters and setters is only available in browsers using the mozilla gecko engine.
And, no, I don't see how you would "hack" this for other browsers.
-
yeah, i know, thats what Im saying...
I do believe there is a way and Im going to figure it out... :)
-
Heres my first attempt:
It works great, but Im weary of the CPU setIntervals can consume.
Code:
Object.prototype.addProperty = function($prop, $getter, $setter) {
this[$prop] = "";
this.getter = $getter;
this.setter = $setter;
var _this = this;
this.listener = setInterval(function() {
if(_this[$prop] != _this.getter()) {
_this.setter(_this[$prop]);
_this[$prop] = _this.getter()
}
},30)
}
Code:
Useage ::
// Object.addProperty(property:String, getter:Function, setter:Function);
myDiv.addProperty("_x",function() { return this.style.left; }, function(x) { this.style.left = x+'px';});
This could be a perfect solution without the setInterval. Any ideas?