10 Replies Latest reply on Sep 15, 2008 11:10 PM by slaingod

    hasOwnProperty only for public?

    slaingod Level 1
      Trying to optionally call a method only if it exists:

      private function _upload():void {
      // first see if we have any custom handling that needs to be done...
      if(this.hasOwnProperty('upload') && this['upload']() == false) {
      // custom handling told us to stop processing!
      return;
      }
      ...
      }
      For some reason upload() only gets called if it is public. Is this how it is supposed to work, or are the names mangled or something? Is there some other way to test for a method that works for protected/private methods/variables?

      Thanks.
        • 1. Re: hasOwnProperty only for public?
          Level 7

          "slaingod" <webforumsuser@macromedia.com> wrote in message
          news:gak3lh$jr5$1@forums.macromedia.com...
          > Trying to optionally call a method only if it exists:
          >
          > private function _upload():void {
          > // first see if we have any custom handling that needs to be done...
          > if(this.hasOwnProperty('upload') && this['upload']() == false) {
          > // custom handling told us to stop processing!
          > return;
          > }
          > ...
          > }
          > For some reason upload() only gets called if it is public. Is this how it
          > is
          > supposed to work? Is there some other way to test for a method that works
          > for
          > protected/private methods/variables?

          I'm a bit confused. Are you saying that _upload() only gets called, or are
          you saying that the upload (no underscore) property is a getter/setter pair,
          and those functions are not getting called? Where are you trying to call it
          from? I assume you're not trying to call the function from outside the
          component...?


          • 2. Re: hasOwnProperty only for public?
            slaingod Level 1
            No, I am saying that hasOwnPropety is returning false for private/protected methods. For some reason the method has to be public to return true for hasOwnPropety. (This has nothing to do with get/set properties.)

            private function MyFunction():void {
            trace('Here');
            }

            this.hasOwnProperty('MyFunction') should return true according to the docs, but it doesn't (though docs aren't always correct obviously).

            AS3 function hasOwnProperty(name:String):Boolean

            ...The following types of properties cause this method to return true for objects that are instances of a class (as opposed to class objects):

            * Fixed instance properties—variables, constants, or ==>methods<== defined by the object's class that are not static;
            _________________________

            Nowhere does it mention these must be public methods. Is there some other way to detect private methods that is built-in?

            I guess I could do something like:

            function has(prop:String) {
            try {
            this[prop]
            return true;
            } catch(e:Error) {
            return false;
            }
            }

            and had that to Object.


            • 3. Re: hasOwnProperty only for public?
              Level 7

              "slaingod" <webforumsuser@macromedia.com> wrote in message
              news:gal7uc$rdo$1@forums.macromedia.com...
              > No, I am saying that hasOwnPropety is returning false for
              > private/protected
              > methods. For some reason the method has to be public to return true for
              > hasOwnPropety. (This has nothing to do with get/set properties.)
              >
              > private function MyFunction():void {
              > trace('Here');
              > }
              >
              > this.hasOwnProperty('MyFunction') should return true according to the
              > docs,
              > but it doesn't (though docs aren't always correct obviously).
              >
              > AS3 function hasOwnProperty(name:String):Boolean
              >
              > ...The following types of properties cause this method to return true for
              > objects that are instances of a class (as opposed to class objects):
              >
              > * Fixed instance properties?variables, constants, or ==>methods<==
              > defined
              > by the object's class that are not static;
              > _________________________
              >
              > Nowhere does it mention these must be public methods. Is there some other
              > way
              > to detect private methods that is built-in?
              >
              > I guess I could do something like:
              >
              > function has(prop:String) {
              > try {
              > this[prop]
              > return true;
              > } catch(e:Error) {
              > return false;
              > }
              > }
              >
              > and had that to Object.

              The code that you showed didn't have an upload() method, but was looking at
              this['upload'], which would be a property. I was trying to get
              clarification on exactly what was going on in your code. Since you didn't
              actually provide clarification on what your code is actually doing, but
              instead simply reiterated what your _conclusions_ were, I'm afraid I can't
              help you any more. Good luck!


              • 4. hasOwnProperty only for public?
                slaingod Level 1
                Yea I think you don't understand how things work with properties. By it's very nature a property is a method. In many cases a property points to an underlying instance variable, but this is not required.

                this['upload'] returns a Function pointer, if there is an upload(...) method in the class.

                this['upload'](some, args); calls it.

                My goal is to check to see if the function or property exists BEFORE I call it/use it. Object#hasOwnProperty() works for public variables/methods, but it appears that Flex hides private and protected variables and methods for some reason, though this does not follow from the documentation. Unless UIComponent or something else is overriding hasOwnProperty for its own purposes, to hide internal variables, or unless name mangling occurs in the compiler. Or it could be that 'property' means public by its very nature as well. :)

                • 5. Re: hasOwnProperty only for public?
                  Level 7

                  "slaingod" <webforumsuser@macromedia.com> wrote in message
                  news:gam1gg$oim$1@forums.macromedia.com...
                  > Yea I think you don't understand how things work with properties. By it's
                  > very
                  > nature a property is a method. In many cases a property points to an
                  > underlying instance variable, but this is not required.

                  Or maybe I am just trying to figure out what your code contains and how you
                  are using the functionality. When you're asking for help, you need to
                  actually provide enough details for people to help you. When you don't, and
                  people ask you to clarify, they are going out of their way for you in a way
                  they don't have to--the onus is on _you_ to make sure you give enough
                  details so people can help you. The least you can do is respect that
                  effort.

                  > this['upload'] returns a Function pointer, if there is an upload(...)
                  > method
                  > in the class.
                  >
                  > this['upload'](some, args); calls it.
                  >
                  > My goal is to check to see if the function or property exists BEFORE I
                  > call
                  > it/use it. Object#hasOwnProperty() works for public variables/methods, but
                  > it
                  > appears that Flex hides private and protected variables and methods for
                  > some
                  > reason, though this does not follow from the documentation. Unless
                  > UIComponent
                  > or something else is overriding hasOwnProperty for its own purposes, to
                  > hide
                  > internal variables, or unless name mangling occurs in the compiler.

                  Why doesn't your class know what properties and methods it has? I think the
                  reason that hasOwnProperty and describeType don't tell you about the
                  internals of the class is that they are looking at the API the class
                  publishes. I don't think they're intended to tell a class what internal
                  variables/functions it has, since it should already know.


                  • 6. Re: hasOwnProperty only for public?
                    ntsiii Level 3
                    I don't know for sure, but I suspect that "... that 'property' means public by its very nature ..." is correct. It's primary use is from outside an instance to determine if a public property is available.

                    Perhaps "describeType()" would help in your case, but i understand it is fairly "expensive".

                    Tracy
                    • 7. Re: hasOwnProperty only for public?
                      slaingod Level 1
                      Thanks, ntsii,
                      Yea I've just bit the bullet and made everything public. Basically this is a poor man's subclassing using includes.

                      Amy,
                      Sorry if you somehow took offense at what I typed, but my post count is over 300 and most of those are me answering questions on this forum, so I'm pretty sure how it works.
                      I thought my original question was pretty straightforward to anyone who might know the answer.

                      To explain my use case:

                      I make a number of sites that have a large amount of overlap in the 'user uploads' area, examples like:

                      http://www.pinktogether.com
                      http://www.klondikecontest.com/videos.aspx
                      http://www.benjerry.com/imagine/
                      http://posewood2.ugenmedia.com

                      As such, a core piece of functionality stays (mostly) the same for each site, so I have moved that core functionality into shared include files. So if a component needs registration functionality, I just include Register.as. If it also needs upload functionality, I also include Upload.as.

                      Internally in these shared includes, the code makes calls like:
                      //public function upload():Boolean
                      public function _upload():void {
                      // first see if we have any custom handling that needs to be done...
                      if(this.hasOwnProperty('upload') && this['upload']() == false) {
                      // custom handling told us to stop processing!
                      return;
                      }

                      ....(default code here)....
                      }

                      This cuts down on the cut and paste tremendously that would be required by using subclasses, etc. There may be some esoteric way of doing this 'right' using interfaces, classes, etc., but this is a very cheap way of achieving 90% of the code reuse I want, at a very low technical barrier for others who might need to use my code as well.

                      Though I also think Cairngorm is the devil (sort of). :)
                      • 8. Re: hasOwnProperty only for public?
                        Level 7

                        "ntsiii" <tspratt@lariatinc.com> wrote in message
                        news:gama4f$4sr$1@forums.macromedia.com...
                        >I don't know for sure, but I suspect that "... that 'property' means public
                        >by
                        > its very nature ..." is correct. It's primary use is from outside an
                        > instance
                        > to determine if a public property is available.
                        >
                        > Perhaps "describeType()" would help in your case, but i understand it is
                        > fairly "expensive".

                        DescribeTypeCache isn't, but it doesn't get private properties either.


                        • 9. Re: hasOwnProperty only for public?
                          Level 7

                          "slaingod" <webforumsuser@macromedia.com> wrote in message
                          news:gambul$6ua$1@forums.macromedia.com...
                          > Thanks, ntsii,
                          > Yea I've just bit the bullet and made everything public. Basically this is
                          > a
                          > poor man's subclassing using includes.
                          >
                          > Amy,
                          > Sorry if you somehow took offense at what I typed, but my post count is
                          > over
                          > 300 and most of those are me answering questions on this forum, so I'm
                          > pretty
                          > sure how it works.

                          Remember, everyone who posts here doesn't just automagically know who you
                          are and what your level of expertise is, so it's completely reasonable to
                          try to eliminate the possibility you're not trying to access private methods
                          from outside the class and to ensure there actually _is_ an upload property
                          to look at. Not everyone on here is at a level where it's possible to
                          automatically assume that their assumptions about what they are seeing are
                          right. In fact, 90+% of the time on here when posters think they know why
                          something is happening, they are wrong.

                          > I thought my original question was pretty straightforward to anyone who
                          > might
                          > know the answer.
                          >
                          > To explain my use case:
                          >
                          > I make a number of sites that have a large amount of overlap in the 'user
                          > uploads' area, examples like:
                          >
                          > http://www.pinktogether.com
                          > http://www.klondikecontest.com/videos.aspx
                          > http://www.benjerry.com/imagine/
                          > http://posewood2.ugenmedia.com
                          >
                          > As such, a core piece of functionality stays (mostly) the same for each
                          > site,
                          > so I have moved that core functionality into shared include files. So if
                          > a
                          > component needs registration functionality, I just include Register.as.
                          > If it
                          > also needs upload functionality, I also include Upload.as.
                          >
                          > Internally in these shared includes, the code makes calls like:
                          > //public function upload():Boolean
                          > public function _upload():void {
                          > // first see if we have any custom handling that needs to be done...
                          > if(this.hasOwnProperty('upload') && this['upload']() == false) {
                          > // custom handling told us to stop processing!
                          > return;
                          > }
                          >
                          > ....(default code here)....
                          > }
                          >
                          > This cuts down on the cut and paste tremendously that would be required by
                          > using subclasses, etc. There may be some esoteric way of doing this
                          > 'right'
                          > using interfaces, classes, etc., but this is a very cheap way of achieving
                          > 90%
                          > of the code reuse I want, at a very low technical barrier for others who
                          > might
                          > need to use my code as well.

                          You may want to look at the decorator pattern.

                          HTH;

                          Amy


                          • 10. Re: hasOwnProperty only for public?
                            slaingod Level 1
                            I haven't been around much in the last couple of months or so, but I wasn't aware of the 'Newsgroup' changes. I am posting on the website.