SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    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...

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    myDiv.someName = function(){alert("hello");}

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    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.

  4. #4
    SitePoint Member
    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...

  5. #5
    SitePoint Member
    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';});
    This could be a perfect solution without the setInterval. Any ideas?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •