Results 1 to 8 of 8

Thread: Custom Object "getter setter"

  1. #1
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Custom Object "getter setter"

    Hi,

    Im building custom 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 it only works in Mozilla type browsers. It doesn't work in Safari! Arrrg!

    Doesn anyone know a hack?

    Thanks!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, not really. Setters and getters aren't supported in a lot of browsers. You'll just have to use getX() and setX(newX).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yeah I'm using functions for this which work perfectly. Im sooo picky.

    There has so be some way (bescides using a setInterval, thats cheating) to do this. Maybe I'll dismantle the form onchanged method. Its a start

  4. #4
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't understand what your problem is with JavaBeans-like getX() and setX(x).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I dont have a problem with functions? I prob should of mentioned because it does sounds like over kill just to change syntax from _x() to _x, but its actually important to the project.

    I'm a Flash designer/devleoper professionally and I wrote a JS class that allows you to write syntax just like Actionscript using javascript / AJAX. (Javascript with Actionscript Writing Syntax : "JAWS") Its really great and multi-browser-compatable. (except for the damn AS properties)

    I did write them all as _x() _y() _alpha() etc.. which works, but its the only difference in syntax and I'm not stopping at 99% complete.

    If I can't get a better method than the addProperty proto I wrote, I have a plan B.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, I see. Well... no, I don't think you're going to get much better than that, but I can see it leading to race conditions. I'm afraid you might have to admit defeat on this one.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Race conditions?

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
  •