5 Replies Latest reply on May 11, 2010 3:26 AM by pauland

    Getters and Setters in ActionScript and ECMAScript

    kiran7881 Level 1

      Hi ,

      This line is taken from a book , here the Author mentions that

       

      In ActionScript , getters and setters is not necessary , they are a part of of the core ECMAScript language.

       

      Can anybody please let me know what is meant by ECMAScript language ??

      Thanks in advance .

        • 1. Re: Getters and Setters in ActionScript and ECMAScript
          Karl_Sigiscar_1971 Level 3

          ECMAScript is an ECMA standard.

           

          Getters and setters are not mandatory (that's what they mean).

           

          However, they can be useful in many cases.

           

          - You can check the value in a setter before assigning (e.g checking if it's null)

          - You cannot override properties of a superclass but you can getters/setters just as you can methods

          - You can take some action when the value of the property is changed. There are many examples in UIComponent classes of the Flex framework.

          - You can put a breakpoint in a setter in order to debug data binding (see if it triggers)

          - In order to optimize data binding, you can write [Bindable event="YourCustomEventNameHere")] before the getter/setter and dispatch this event when the value is set so as to enhance performance of data binding (if you just write [Bindable], your properties will be processed by the framework even if their value does not change).

           

          Also, note how the synthax is different than Java.

           

          // ActionScript

           

          private var _myProperty:uint;

           

          public function get myProperty():uint

          {

            return _myProperty;

          }

           

          public function set myProperty(value:uint):void

          {

            _myProperty = value;

          }

           

          // Java

           

          private Integer myProperty;

           

          public Integer getMyProperty():

          {

            return myProperty;

          }

           

          public void setMyProperty(value:Integer):

          {

            myProperty = value;

          }

           

          Avoid creating getters/setters a la Java in ActionScript (i.e not using the get/set keywords). This is something I have seen in some implementations or auto-generated code from UML.

          1 person found this helpful
          • 2. Re: Getters and Setters in ActionScript and ECMAScript
            kiran7881 Level 1

            You can check the value in a setter before assigning (e.g checking if it's null) .

             

            Thanks Karl , for the above valuable point .

            I suggest this is inside the Actioncript class . I want a similar behaviour onto my Action class , for example there is a property called as name

            and if the user tries to enter name less than 4 characters it should throw an exception .

             

            public class MyClass
            {
            private var name:String 
            public function get name():String {
                return name;
            }
            public function set name(name:String):void {
              

            if(name.length < 5)
            //  i want to throw an Exception  here
            }

            else

            name = name;

             

            }
            }

             

            I dont have any java class associated with this ActionScript class , Please tell me whetehr this is possible to achive this kind of behaviour .

            Thanks in advance .

            • 3. Re: Getters and Setters in ActionScript and ECMAScript
              Navin Mathew

              yes it can be done in flex. you might want to change the name of the parameter of the setter to something else to avoid confusion.

              • 4. Re: Getters and Setters in ActionScript and ECMAScript
                kiran7881 Level 1

                Hi Puli ,

                 

                I didn't understand what you mentioned as why to change the setter name ,

                 

                can you please provide a sample of it what you said .

                 

                Thanks in advance

                • 5. Re: Getters and Setters in ActionScript and ECMAScript
                  pauland Level 4

                  Having a class variable "name" with a parameter called "name" invites confusion (particularly when the setter has the same name), particularly when you write

                   

                  "name = name;"

                   

                  You can explicitly reference the class variable using "this.name" but you may wish to think about adopting a convention for parameter naming, particularly for setters.

                   

                  I generally name setter parameters "newValue" and adopt a parameter naming convention using "$" as a prefix.

                   

                  Using this convention your assignment would then become "name=$newValue" which would lose any confusion versus "name=name". You may wish to use this convention or have one of our own, but either way the existing example "name=name" needs a tweak.

                   

                  I should also say that I also use getter and setters with private class variables and a good convention is to use "_" as a prefix to indicate that the variable is private. This would make the example "_name=$newValue" which make everything clear because there is no ambiguity between any of the variables involved or the setter name.

                   

                  Paul