SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Custom "getter setter"
-
Mar 17, 2007, 23:16 #1
- Join Date
- Mar 2007
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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...
-
Mar 18, 2007, 01:58 #2
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
myDiv.someName = function(){alert("hello");}
-
Mar 18, 2007, 06:39 #3
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Mar 18, 2007, 09:53 #4
- Join Date
- Mar 2007
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yeah, i know, thats what Im saying...
I do believe there is a way and Im going to figure it out...
-
Mar 18, 2007, 11:10 #5
- Join Date
- Mar 2007
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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';});
Bookmarks