[Flashcoders] Q:getter setters vs accessor methods
Hi Just wondering if there was a consensus on which use of getter/setters is used most often traditional getter/setters ie public function get Size():Number{} or accessor methods public function getSize():Number{} Which do you prefer and why...? [e] jbach at bitstream.ca [c] 416.668.0034 [w] www.bitstream.ca ...all improvisation is life in search of a style. - Bruce Mau,'LifeStyle' ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: Re[4]: [Flashcoders] Q:getter setters vs accessor methods
The line in the constructor: this._size = 2; is calling the setter. Maybe you meant to call this.__size = 2; Setters will trigger regardless of whether the property is set by code within the class itself or from external code. Nathan http://www.nathanderksen.com On Feb 8, 2006, at 5:31 PM, Iv wrote: Hello JesterXL, my test shows anoter result and add new question: -who called getter in the code? class Test { private var __size:Number = 0; function Test() { this._size = 2; } public function get _size():Number { trace(get _size); return this.getSize(); } public function set _size(new_size:Number):Void { trace(set _size); this.setSize(new_size); trace(end set _size\n); } // private function setSize(new_size:Number):Number { trace(setSize); this.__size = new_size; return new_size; } private function getSize():Number { trace(getSize); return this.__size; } } fla: foo = new Test() output: set _size setSize end set _size get _size getSize J size = 2 does: J - setter runs J - getter runs for setter J - getter runs getSize function J - getSize function gets value, returns it J - getter returns value for setter J - setter changes value -- Ivan Dembicki __ __ [EMAIL PROTECTED] || http:// www.design.ru ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: Re[4]: [Flashcoders] Q:getter setters vs accessor methods
Sorry, ignore me, I misread the question. setter != getter Nathan http://www.nathanderksen.com On Feb 8, 2006, at 9:58 PM, Nathan Derksen wrote: The line in the constructor: this._size = 2; is calling the setter. Maybe you meant to call this.__size = 2; Setters will trigger regardless of whether the property is set by code within the class itself or from external code. Nathan http://www.nathanderksen.com On Feb 8, 2006, at 5:31 PM, Iv wrote: Hello JesterXL, my test shows anoter result and add new question: -who called getter in the code? class Test { private var __size:Number = 0; function Test() { this._size = 2; } public function get _size():Number { trace(get _size); return this.getSize(); } public function set _size(new_size:Number):Void { trace(set _size); this.setSize(new_size); trace(end set _size\n); } // private function setSize(new_size:Number):Number { trace(setSize); this.__size = new_size; return new_size; } private function getSize():Number { trace(getSize); return this.__size; } } fla: foo = new Test() output: set _size setSize end set _size get _size getSize J size = 2 does: J - setter runs J - getter runs for setter J - getter runs getSize function J - getSize function gets value, returns it J - getter returns value for setter J - setter changes value -- Ivan Dembicki _ ___ [EMAIL PROTECTED] || http:// www.design.ru ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Q:getter setters vs accessor methods
Right. And in simple assignment, getter's returned value is returned as the assignment formula's value. According to ECMA-262 (11.13.1), the formula's value should be its lefthand expression. var _mc:MovieClip = this.createEmptyMovieClip(my_mc, 1); var a:Number = _mc._rotation=270; trace([a, _mc._rotation]); // Output: 270,-90 // Good var o:MyRotation = new MyRotation(); var b:Number = o.rotation=-90; trace([b, o.rotation]); // Output: 270,270 // Bad // ActionScript 2.0 class definition for Testing class MyRotation { var _rotation:Number = 0; // gets the value between 0 and 360 function get rotation():Number { trace([get, _rotation]); return (_rotation%360+360)%360; } // sets the value between -180 and 180 function set rotation(n:Number):Void { _rotation = (n%360+540)%360-180; trace([set, _rotation]); } } _ JesterXL wrote: Setters call the getters automatically. Good luck, Fumio Nonaka mailto:[EMAIL PROTECTED] http://www.FumioNonaka.com/ My bookshttp://www.FumioNonaka.com/Books/index.html Flash communityhttp://F-site.org/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Q:getter setters vs accessor methods
Sorry. The formula's value should be its assignment expression, i.e. righthand side expression. _ Fumio Nonaka wrote: Right. And in simple assignment, getter's returned value is returned as the assignment formula's value. According to ECMA-262 (11.13.1), the formula's value should be its lefthand expression. Good luck, Fumio Nonaka mailto:[EMAIL PROTECTED] http://www.FumioNonaka.com/ My bookshttp://www.FumioNonaka.com/Books/index.html Flash communityhttp://F-site.org/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders